vlcPlayer是一款免费开源的播放器项目,可以播放几乎所有的视频格式。
第一步:获取dll
安装vlcplayer视频播放器,在安装目录下面获取所需的dll。
dll文件夹:plugins
还有2个dll:libvlc.dll,libvlccore.dll
第二步:获取播放器对象基类VlcPlayerBase
using System; using System.Runtime.InteropServices; using System.Security; using System.Text; namespace VlcPlayer { public class VlcPlayerBase { private IntPtr libvlc_instance_; private IntPtr libvlc_media_player_; ////// 视频时长 /// private double duration_; /// /// VLC 播放器。 /// /// public VlcPlayerBase(string pluginPath) { //string pluginPath = Environment.CurrentDirectory + "\\vlc\\plugins\\"; //插件目录 string plugin_arg = "--plugin-path=" + pluginPath; string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg }; libvlc_instance_ = LibVlcAPI.libvlc_new(arguments); libvlc_media_player_ = LibVlcAPI.libvlc_media_player_new(libvlc_instance_); //创建 libvlc_media_player 播放核心 } /// /// 设置播放容器 /// /// 播放容器句柄 public void SetRenderWindow(int wndHandle) { if (libvlc_instance_ != IntPtr.Zero && wndHandle != 0) { LibVlcAPI.libvlc_media_player_set_hwnd(libvlc_media_player_, wndHandle); //设置播放容器 } } /// /// 播放指定媒体文件 /// /// public void LoadFile(string filePath) { IntPtr libvlc_media = LibVlcAPI.libvlc_media_new_path(libvlc_instance_, filePath); //创建 libvlc_media_player 播放核心 if (libvlc_media != IntPtr.Zero) { LibVlcAPI.libvlc_media_parse(libvlc_media); duration_ = LibVlcAPI.libvlc_media_get_duration(libvlc_media) / 1000.0; //获取视频时长 LibVlcAPI.libvlc_media_player_set_media(libvlc_media_player_, libvlc_media); //将视频绑定到播放器去 LibVlcAPI.libvlc_media_release(libvlc_media); //LibVlcAPI.libvlc_media_player_play(libvlc_media_player_); //播放 } } /// /// 播放 /// public void Play() { if (libvlc_media_player_ != IntPtr.Zero) { LibVlcAPI.libvlc_media_player_play(libvlc_media_player_); } } /// /// 暂停播放 /// public void Pause() { if (libvlc_media_player_ != IntPtr.Zero) { LibVlcAPI.libvlc_media_player_pause(libvlc_media_player_); } } /// /// 停止播放 /// public void Stop() { if (libvlc_media_player_ != IntPtr.Zero) { LibVlcAPI.libvlc_media_player_stop(libvlc_media_player_); } } public void Release() { if (libvlc_media_player_ != IntPtr.Zero) { LibVlcAPI.libvlc_media_release(libvlc_media_player_); } } /// /// 获取播放时间进度 /// /// public double GetPlayTime() { return LibVlcAPI.libvlc_media_player_get_time(libvlc_media_player_) / 1000.0; } /// /// 设置播放时间 /// /// public void SetPlayTime(double seekTime) { LibVlcAPI.libvlc_media_player_set_time(libvlc_media_player_, (Int64)(seekTime * 1000)); } /// /// 获取音量 /// /// public int GetVolume() { return LibVlcAPI.libvlc_audio_get_volume(libvlc_media_player_); } /// /// 设置音量 /// /// public void SetVolume(int volume) { LibVlcAPI.libvlc_audio_set_volume(libvlc_media_player_, volume); } /// /// 设置是否全屏 /// /// public void SetFullScreen(bool istrue) { LibVlcAPI.libvlc_set_fullscreen(libvlc_media_player_, istrue ? 1 : 0); } /// /// 视频时长 /// /// public double Duration { get { return duration_; } } /// /// 是否正在播放 /// public bool IsPlaying { get { if (Duration > 0 && (int)GetPlayTime() == (int)Duration) this.Stop(); //如果播放完,关闭视频 return (int)GetPlayTime() < (int)Duration /* 播放时间进度小于视频时长 */ && Duration > 0 /* 播放时间进度大于0 */ && GetPlayTime() > 0; /* 视频时长大于0 */ } } /// /// 获取版本(VS2015 调试模式程序会直接崩掉) /// /// public string Version { get { return LibVlcAPI.libvlc_get_version(); } } } #region vlclib.dll internal static class LibVlcAPI { internal struct PointerToArrayOfPointerHelper { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)] public IntPtr[] pointers; } /// /// 传入播放参数 /// /// /// public static IntPtr libvlc_new(string[] arguments) { PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper(); argv.pointers = new IntPtr[11]; for (int i = 0; i < arguments.Length; i++) { argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]); //将托管 System.String 中的内容复制到非托管内存,并在复制时转换为 ANSI 格式。 } IntPtr argvPtr = IntPtr.Zero; try { int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper)); //返回非托管类型的大小(以字节为单位)。 argvPtr = Marshal.AllocHGlobal(size); //从进程的非托管内存中分配内存。 Marshal.StructureToPtr(argv, argvPtr, false); //将数据从托管对象封送到非托管内存块。 return libvlc_new(arguments.Length, argvPtr); //创建一个libvlc实例,它是引用计数的 } finally { for (int i = 0; i < arguments.Length + 1; i++) { if (argv.pointers[i] != IntPtr.Zero) { Marshal.FreeHGlobal(argv.pointers[i]); //释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。 } } if (argvPtr != IntPtr.Zero) { Marshal.FreeHGlobal(argvPtr);/* 释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。 */ } } } /// /// 从本地文件系统路径新建,其他参照上一条 /// /// /// /// public static IntPtr libvlc_media_new_path(IntPtr libvlc_instance, string path) { IntPtr pMrl = IntPtr.Zero; try { byte[] bytes = Encoding.UTF8.GetBytes(path); pMrl = Marshal.AllocHGlobal(bytes.Length + 1); Marshal.Copy(bytes, 0, pMrl, bytes.Length); Marshal.WriteByte(pMrl, bytes.Length, 0); return libvlc_media_new_path(libvlc_instance, pMrl); // 从本地文件路径构建一个libvlc_media } finally { if (pMrl != IntPtr.Zero) { Marshal.FreeHGlobal(pMrl);/* 释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。 */ } } } /// /// 使用一个给定的媒体资源路径来建立一个libvlc_media对象.参数psz_mrl为要读取的MRL(Media Resource Location).此函数返回新建的对象或NULL. /// /// /// /// public static IntPtr libvlc_media_new_location(IntPtr libvlc_instance, string path) { IntPtr pMrl = IntPtr.Zero; try { byte[] bytes = Encoding.UTF8.GetBytes(path); pMrl = Marshal.AllocHGlobal(bytes.Length + 1); Marshal.Copy(bytes, 0, pMrl, bytes.Length); Marshal.WriteByte(pMrl, bytes.Length, 0); return libvlc_media_new_path(libvlc_instance, pMrl); // 从本地文件路径构建一个libvlc_media } finally { if (pMrl != IntPtr.Zero) { Marshal.FreeHGlobal(pMrl);/* 释放以前使用 System.Runtime.InteropServices.Marshal.AllocHGlobal(System.IntPtr) 从进程的非托管内存中分配的内存。 */ } } } // ---------------------------------------------------------------------------------------- // 以下是libvlc.dll导出函数 /// /// 创建一个libvlc实例,它是引用计数的 /// /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] private static extern IntPtr libvlc_new(int argc, IntPtr argv); /// /// 释放libvlc实例 /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_release(IntPtr libvlc_instance); /// /// 获取版本 /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern String libvlc_get_version(); /// /// 从视频来源(例如Url)构建一个libvlc_meida /// /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path); /// /// 从本地文件路径构建一个libvlc_media /// /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path); /// /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_release(IntPtr libvlc_media_inst); /// /// 创建libvlc_media_player(播放核心) /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance); /// /// 将视频(libvlc_media)绑定到播放器上 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media); /// /// 设置图像输出的窗口 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable); #region 播放控制 /// /// 播放 /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer); /// /// 暂停 /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer); /// /// 停止 /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer); #endregion /// /// 释放播放文件 /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer); /// /// 解析视频资源的媒体信息(如时长等) /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_parse(IntPtr libvlc_media); /// /// 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效) /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media); #region 播放时间进度 /// /// 当前播放的时间进度 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer); /// /// 设置播放位置(拖动) /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time); #endregion #region 音量 /// /// 获取音量 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player); /// /// 设置音量 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume); #endregion /// /// 设置全屏 /// /// /// [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen); /// /// 获取播放状态。(Win10 不支持) /// /// /// //[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] //[SuppressUnmanagedCodeSecurity] //public static extern Int64 libvlc_media_player_get_state(IntPtr libvlc_mediaplayer); } #endregion }
第三步:写代码实现
string pluginPath = Environment.CurrentDirectory + "\\plugins\\"; //插件目录 player = new VlcPlayerBase(pluginPath); player.SetRenderWindow((int)pnlVideo.Handle);//panel player.LoadFile(videoName[0]);//视频文件路径
设置音量:
int volume = this.player.GetVolume();
volume--;
if (volume <0) volume = 0;
this.player.SetVolume(volume);
还有播放状态设置:
this.player.Play();
this.player.Pause();
this.player.Stop();
其他设置:
int durationSecond = (int)this.player.Duration;
int playTimeSecond = (int)this.player.GetPlayTime();
this.player.SetPlayTime(this.tkbProgress.Value);