技术开发 频道

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

  下面对这些操作进行封装,以供上层使用。

1     class VlcPlayer
2     {
3         private IntPtr libvlc_instance_;
4         private IntPtr libvlc_media_player_;
5
6         private double duration_;
7
8         public VlcPlayer(string pluginPath)
9         {
10             string plugin_arg = "--plugin-path=" + pluginPath;
11             string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg };
12             libvlc_instance_ = LibVlcAPI.libvlc_new(arguments);
13
14             libvlc_media_player_ = LibVlcAPI.libvlc_media_player_new(libvlc_instance_);
15         }
16
17         public void SetRenderWindow(int wndHandle)
18         {
19             if (libvlc_instance_ != IntPtr.Zero && wndHandle != 0)
20             {
21 LibVlcAPI.libvlc_media_player_set_hwnd(libvlc_media_player_, wndHandle);
22             }
23         }
24
25         public void PlayFile(string filePath)
26         {
27             IntPtr libvlc_media = LibVlcAPI.libvlc_media_new_path(libvlc_instance_, filePath);
28             if (libvlc_media != IntPtr.Zero)
29             {
30                 LibVlcAPI.libvlc_media_parse(libvlc_media);
31                 duration_ = LibVlcAPI.libvlc_media_get_duration(libvlc_media) / 1000.0;
32
33                 LibVlcAPI.libvlc_media_player_set_media(libvlc_media_player_, libvlc_media);
34                 LibVlcAPI.libvlc_media_release(libvlc_media);
35
36                 LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);
37             }
38         }
39
40         public void Pause()
41         {
42             if (libvlc_media_player_ != IntPtr.Zero)
43             {
44                 LibVlcAPI.libvlc_media_player_pause(libvlc_media_player_);
45             }
46         }
47
48         public void Stop()
49         {
50             if (libvlc_media_player_ != IntPtr.Zero)
51             {
52                 LibVlcAPI.libvlc_media_player_stop(libvlc_media_player_);
53             }
54         }
55
56         public double GetPlayTime()
57         {
58             return LibVlcAPI.libvlc_media_player_get_time(libvlc_media_player_) / 1000.0;
59         }
60
2
相关文章