【IT168技术】本人之前很多的文章中均提到了使用libvlc为播放器内核制作的播放器,也许有些朋友对此感兴趣,于是我用c#写了一个调用libvlc api实现的功能较多视频播放器,与大家分享一下。说它“功能较多”,当然是因为我们站在了vlc的肩膀上。
vlc是一个强大而且开源的多媒体播放器,也可以说是一个多媒体平台。它支持非常广泛的媒体格式的本地播放,完全可以媲美mplayer,其对视频网络流的处理能力更是非常强悍。libvlc就是指的vlc的核心,它向外提供了一系列的接口,通过接口,来实现视频播放等复杂的功能。libvlc对外提供了c语言的接口,也有其他语言,包括.net的绑定,在其官网上就有,不过已经“年久失修”。我之前用Qt, MFC实现过基于libvlc的播放器,不过鉴于园子里c#开发人员较多,遂用c#封装了一下libvlc的API接口,并实现了一个视频播放器。
先上鉴赏图,外表很简单,不过,外表不是重点:
首先是libvlc的一些导出函数,我在注释里对它们的功能都有说明
1 // 创建一个libvlc实例,它是引用计数的
2 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
3 [SuppressUnmanagedCodeSecurity]
4 private static extern IntPtr libvlc_new(int argc, IntPtr argv);
5
6 // 释放libvlc实例
7 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
8 [SuppressUnmanagedCodeSecurity]
9 public static extern void libvlc_release(IntPtr libvlc_instance);
10
11 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
12 [SuppressUnmanagedCodeSecurity]
13 public static extern String libvlc_get_version();
14
15 // 从视频来源(例如Url)构建一个libvlc_meida
16 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
17 [SuppressUnmanagedCodeSecurity]
18 private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path);
19
20 // 从本地文件路径构建一个libvlc_media
21 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
22 [SuppressUnmanagedCodeSecurity]
23 private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);
24
25 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
26 [SuppressUnmanagedCodeSecurity]
27 public static extern void libvlc_media_release(IntPtr libvlc_media_inst);
28
29 // 创建libvlc_media_player(播放核心)
30 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
31 [SuppressUnmanagedCodeSecurity]
32 public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);
33
34 // 将视频(libvlc_media)绑定到播放器上
35 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
36 [SuppressUnmanagedCodeSecurity]
37 public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media);
38
2 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
3 [SuppressUnmanagedCodeSecurity]
4 private static extern IntPtr libvlc_new(int argc, IntPtr argv);
5
6 // 释放libvlc实例
7 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
8 [SuppressUnmanagedCodeSecurity]
9 public static extern void libvlc_release(IntPtr libvlc_instance);
10
11 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
12 [SuppressUnmanagedCodeSecurity]
13 public static extern String libvlc_get_version();
14
15 // 从视频来源(例如Url)构建一个libvlc_meida
16 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
17 [SuppressUnmanagedCodeSecurity]
18 private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path);
19
20 // 从本地文件路径构建一个libvlc_media
21 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
22 [SuppressUnmanagedCodeSecurity]
23 private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);
24
25 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
26 [SuppressUnmanagedCodeSecurity]
27 public static extern void libvlc_media_release(IntPtr libvlc_media_inst);
28
29 // 创建libvlc_media_player(播放核心)
30 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
31 [SuppressUnmanagedCodeSecurity]
32 public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);
33
34 // 将视频(libvlc_media)绑定到播放器上
35 [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
36 [SuppressUnmanagedCodeSecurity]
37 public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media);
38