技术开发 频道

C#:libvlc实现功能较多视频播放器

39         // 设置图像输出的窗口
40         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
41         [SuppressUnmanagedCodeSecurity]
42         public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable);
43
44         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
45         [SuppressUnmanagedCodeSecurity]
46         public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer);
47
48         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
49         [SuppressUnmanagedCodeSecurity]
50         public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer);
51
52         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
53         [SuppressUnmanagedCodeSecurity]
54         public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer);
55
56         // 解析视频资源的媒体信息(如时长等)
57         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
58         [SuppressUnmanagedCodeSecurity]
59         public static extern void libvlc_media_parse(IntPtr libvlc_media);
60
61         // 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效)
62         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
63         [SuppressUnmanagedCodeSecurity]
64         public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media);
65
66         // 当前播放的时间
67         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
68         [SuppressUnmanagedCodeSecurity]
69         public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer);
70
71         // 设置播放位置(拖动)
72         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
73         [SuppressUnmanagedCodeSecurity]
74         public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time);
75
76         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
77         [SuppressUnmanagedCodeSecurity]
78         public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer);
79
80         // 获取和设置音量
81         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
82         [SuppressUnmanagedCodeSecurity]
83         public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player);
84
85         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
86         [SuppressUnmanagedCodeSecurity]
87         public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume);
88
89         // 设置全屏
90         [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
91         [SuppressUnmanagedCodeSecurity]
92         public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);

  要使用libvlc api进行播放,首先需要创建一个libvlc的实例,就是lbvlc_instance,之后所有的操作都是基于这个实例来完成。

  每一个播放窗口,对应一个libvlc_media_player,而每一个媒体文件,就是一个libvlc_media。所以,调用的步骤就是:

  ①创建libvlc_instance

  ②创建libvlc_media_player

  ③开始播放时,创建libvlc_media,通知libvlc_media_player要播放的媒体文件,之后就可以释放libvlc_media了

  ④操作libvlc_media_player,实现播放、暂停等功能

2
相关文章