C# vlc播放视频
一、方法方法一(winform实现):
安装VLC media player3.0,然后将安装的plugins文件复制到项目根目录,
1、新建一个类(VlcPlayerBase.cs):
using System; using System.Runtime.InteropServices; using System.Security; using System.Text; using System.Threading; namespace WinVlcProject { using libvlc_media_t = System.IntPtr; using libvlc_instance_t = System.IntPtr; using libvlc_media_player_t = System.IntPtr; using static WinVlcProject.LibVlcAPI; public class VlcPlayerBase { private IntPtr libvlc_instance_; private IntPtr libvlc_media_player_; /// <summary> /// 视频时长 /// </summary> private double duration_; /// <summary> /// VLC 播放器。 /// </summary> /// <param name="pluginPath"></param> 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 播放核心 } /// <summary> /// 设置播放容器 /// </summary> /// <param name="wndHandle">播放容器句柄</param> public void SetRenderWindow(int wndHandle) { if (libvlc_instance_ != IntPtr.Zero && wndHandle != 0) { LibVlcAPI.libvlc_media_player_set_hwnd(libvlc_media_player_, wndHandle); //设置播放容器 } } /// <summary> /// 播放指定媒体文件 /// </summary> /// <param name="filePath"></param> 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_); //播放 } } /// <summary> /// 播放指定媒体文件 /// </summary> /// <param name="filePath"></param> public void LoadFileUrl(string filePath) { IntPtr libvlc_media = LibVlcAPI.libvlc_media_new_location(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_); //播放 } } /// <summary> /// 播放 /// </summary> public void Play() { if (libvlc_media_player_ != IntPtr.Zero) { LibVlcAPI.libvlc_media_player_play(libvlc_media_player_); } } /// <summary> /// 暂停播放 /// </summary> public void Pause() { if (libvlc_media_player_ != IntPtr.Zero) { LibVlcAPI.libvlc_media_player_pause(libvlc_media_player_); } } /// <summary> /// 停止播放 /// </summary> 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_); } } /// <summary> /// 获取播放时间进度 /// </summary> /// <returns></returns> public double GetPlayTime() { return LibVlcAPI.libvlc_media_player_get_time(libvlc_media_player_) / 1000.0; } /// <summary> /// 设置播放时间 /// </summary> /// <param name="seekTime"></param> public void SetPlayTime(double seekTime) { LibVlcAPI.libvlc_media_player_set_time(libvlc_media_player_, (Int64)(seekTime * 1000)); } /// <summary> /// 获取音量 /// </summary> /// <returns></returns> public int GetVolume() { return LibVlcAPI.libvlc_audio_get_volume(libvlc_media_player_); } /// <summary> /// 设置音量 /// </summary> /// <param name="volume"></param> public void SetVolume(int volume) { LibVlcAPI.libvlc_audio_set_volume(libvlc_media_player_, volume); } /// <summary> /// 设置是否全屏 /// </summary> /// <param name="istrue"></param> public void SetFullScreen(bool istrue) { LibVlcAPI.libvlc_set_fullscreen(libvlc_media_player_, istrue ? 1 : 0); } public void Aspect(string aspects) { if (lmpt != IntPtr.Zero) { SafeNativeMethods.libvlc_video_set_aspect_ratio(lmpt, aspects.ToCharArray()); } } /// <summary> /// 视频时长 /// </summary> /// <returns></returns> public double Duration { get { return duration_; } } /// <summary> /// 是否正在播放 /// </summary> public bool IsPlaying { get { if (Duration > 0 && (int)GetPlayTime() == (int)Duration) this.Stop(); //如果播放完,关闭视频 return (int)GetPlayTime() < (int)Duration /* 播放时间进度小于视频时长 */ && Duration > 0 /* 播放时间进度大于0 */ && GetPlayTime() > 0; /* 视频时长大于0 */ } } /// <summary> /// 获取版本(VS2015 调试模式程序会直接崩掉) /// </summary> /// <returns></returns> public string Version { get { return LibVlcAPI.libvlc_get_version(); } } //vlc库启动参数配置 private static string pluginPath = System.Environment.CurrentDirectory + "\\plugins\\"; private static string plugin_arg = "--plugin-path=" + pluginPath; private static string[] arguments = { "-I", "dummy", "--ignore-config", "--video-title", plugin_arg };//, program_arg }; private libvlc_instance_t lit; private libvlc_media_player_t lmpt; /// <summary> /// 播放网络媒体 /// </summary> /// <param name="libvlc_instance">VLC 全局变量</param> /// <param name="libvlc_media_player">VLC MediaPlayer变量</param> /// <param name="url">网络视频URL,支持http、rtp、udp等格式的URL播放</param> /// <returns></returns> private bool NetWork_Media_Play(libvlc_instance_t libvlc_instance, libvlc_media_player_t libvlc_media_player, string url) { IntPtr pMrl = IntPtr.Zero; libvlc_media_t libvlc_media = IntPtr.Zero; try { if (url == null || libvlc_instance == IntPtr.Zero || libvlc_instance == null || libvlc_media_player == IntPtr.Zero || libvlc_media_player == null) { return false; } pMrl = StrToIntPtr(url); if (pMrl == null || pMrl == IntPtr.Zero) { return false; } //播放网络文件 libvlc_media = SafeNativeMethods.libvlc_media_new_location(libvlc_instance, pMrl); if (libvlc_media == null || libvlc_media == IntPtr.Zero) { return false; } //将Media绑定到播放器上 SafeNativeMethods.libvlc_media_player_set_media(libvlc_media_player, libvlc_media); //释放libvlc_media资源 SafeNativeMethods.libvlc_media_release(libvlc_media); libvlc_media = IntPtr.Zero; if (0 != SafeNativeMethods.libvlc_media_player_play(libvlc_media_player)) { return false; } //休眠指定时间 Thread.Sleep(500); return true; } catch (Exception) { //释放libvlc_media资源 if (libvlc_media != IntPtr.Zero) { SafeNativeMethods.libvlc_media_release(libvlc_media); } libvlc_media = IntPtr.Zero; return false; } } //将string []转换为IntPtr private static IntPtr StrToIntPtr(string[] args) { try { IntPtr ip_args = IntPtr.Zero; PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper(); argv.pointers = new IntPtr[11]; for (int i = 0; i < args.Length; i++) { argv.pointers[i] = Marshal.StringToHGlobalAnsi(args[i]); } int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper)); ip_args = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(argv, ip_args, false); return ip_args; } catch (Exception) { return IntPtr.Zero; } } //将string转换为IntPtr private static IntPtr StrToIntPtr(string url) { try { if (string.IsNullOrEmpty(url)) { return IntPtr.Zero; } IntPtr pMrl = IntPtr.Zero; byte[] bytes = Encoding.UTF8.GetBytes(url); pMrl = Marshal.AllocHGlobal(bytes.Length + 1); Marshal.Copy(bytes, 0, pMrl, bytes.Length); Marshal.WriteByte(pMrl, bytes.Length, 0); return pMrl; } catch (Exception) { return IntPtr.Zero; } } /// <summary> /// 播放网络视频流 /// </summary> /// <param name="url">url地址</param> /// <param name="handle">显示控件句柄</param> /// <returns>true:播放成功;false:播放失败</returns> public bool playUrl(string url, IntPtr handle) { lit = Create_Media_Instance(); lmpt = Create_MediaPlayer(lit, handle); //播放网络视频 return NetWork_Media_Play(lit, lmpt, url); //播放本地视频 // return Local_Media_Play(lit, lmpt, url); }/// <summary> /// 创建VLC播放器 /// </summary> /// <param name="libvlc_instance">VLC 全局变量</param> /// <param name="handle">VLC MediaPlayer需要绑定显示的窗体句柄</param> /// <returns></returns> private libvlc_media_player_t Create_MediaPlayer(libvlc_instance_t libvlc_instance, IntPtr handle) { libvlc_media_player_t libvlc_media_player = IntPtr.Zero; try { if (libvlc_instance == IntPtr.Zero || libvlc_instance == null || handle == IntPtr.Zero || handle == null) { return IntPtr.Zero; } //创建播放器 libvlc_media_player = SafeNativeMethods.libvlc_media_player_new(libvlc_instance); if (libvlc_media_player == null || libvlc_media_player == IntPtr.Zero) { return IntPtr.Zero; } //设置播放窗口 SafeNativeMethods.libvlc_media_player_set_hwnd(libvlc_media_player, (int)handle); return libvlc_media_player; } catch { SafeNativeMethods.libvlc_media_player_release(libvlc_media_player); return IntPtr.Zero; } } #region 导入库函数 [SuppressUnmanagedCodeSecurity] internal static class SafeNativeMethods { // 创建一个libvlc实例,它是引用计数的 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern libvlc_instance_t libvlc_new(int argc, IntPtr argv); // 释放libvlc实例 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_release(libvlc_instance_t libvlc_instance); //获取libvlc的版本 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern String libvlc_get_version(); //从视频来源(例如http、rtsp)构建一个libvlc_meida [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern libvlc_media_t libvlc_media_new_location(libvlc_instance_t libvlc_instance, IntPtr path); //从本地文件路径构建一个libvlc_media [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern libvlc_media_t libvlc_media_new_path(libvlc_instance_t libvlc_instance, IntPtr path); //释放libvlc_media [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_media_release(libvlc_media_t libvlc_media_inst); // 创建一个空的播放器 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern libvlc_media_player_t libvlc_media_player_new(libvlc_instance_t libvlc_instance); //从libvlc_media构建播放器 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern libvlc_media_player_t libvlc_media_player_new_from_media(libvlc_media_t libvlc_media); //释放播放器资源 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_media_player_release(libvlc_media_player_t libvlc_mediaplayer); // 将视频(libvlc_media)绑定到播放器上 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_media_player_set_media(libvlc_media_player_t libvlc_media_player, libvlc_media_t libvlc_media); // 设置图像输出的窗口 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_media_player_set_hwnd(libvlc_media_player_t libvlc_mediaplayer, Int32 drawable); //播放器播放 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern int libvlc_media_player_play(libvlc_media_player_t libvlc_mediaplayer); //播放器暂停 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_media_player_pause(libvlc_media_player_t libvlc_mediaplayer); //播放器停止 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_media_player_stop(libvlc_media_player_t libvlc_mediaplayer); // 解析视频资源的媒体信息(如时长等) [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_media_parse(libvlc_media_t libvlc_media); // 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效) [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern Int64 libvlc_media_get_duration(libvlc_media_t libvlc_media); // 当前播放时间 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern Int64 libvlc_media_player_get_time(libvlc_media_player_t libvlc_mediaplayer); // 设置播放时间 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_media_player_set_time(libvlc_media_player_t libvlc_mediaplayer, Int64 time); // 获取音量 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern int libvlc_audio_get_volume(libvlc_media_player_t libvlc_media_player); //设置音量 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_audio_set_volume(libvlc_media_player_t libvlc_media_player, int volume); // 设置全屏 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_set_fullscreen(libvlc_media_player_t libvlc_media_player, int isFullScreen); [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern int libvlc_get_fullscreen(libvlc_media_player_t libvlc_media_player); [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_toggle_fullscreen(libvlc_media_player_t libvlc_media_player); //判断播放时是否在播放 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern bool libvlc_media_player_is_playing(libvlc_media_player_t libvlc_media_player); //判断播放时是否能够Seek [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern bool libvlc_media_player_is_seekable(libvlc_media_player_t libvlc_media_player); //判断播放时是否能够Pause [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern bool libvlc_media_player_can_pause(libvlc_media_player_t libvlc_media_player); //判断播放器是否可以播放 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern int libvlc_media_player_will_play(libvlc_media_player_t libvlc_media_player); //进行快照 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern int libvlc_video_take_snapshot(libvlc_media_player_t libvlc_media_player, int num, char[] filepath, int i_width, int i_height); //获取Media信息 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern libvlc_media_t libvlc_media_player_get_media(libvlc_media_player_t libvlc_media_player); //获取媒体信息 [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern int libvlc_media_get_stats(libvlc_media_t libvlc_media, ref libvlc_media_stats_t lib_vlc_media_stats); [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void libvlc_video_set_aspect_ratio(libvlc_media_player_t libvlc_media_player, char[] aspect); } #endregion #region 结构体 public struct libvlc_media_stats_t { /* Input */ public int i_read_bytes; public float f_input_bitrate; /* Demux */ public int i_demux_read_bytes; public float f_demux_bitrate; public int i_demux_corrupted; public int i_demux_discontinuity; /* Decoders */ public int i_decoded_video; public int i_decoded_audio; /* Video Output */ public int i_displayed_pictures; public int i_lost_pictures; /* Audio output */ public int i_played_abuffers; public int i_lost_abuffers; /* Stream output */ public int i_sent_packets; public int i_sent_bytes; public float f_send_bitrate; } #endregion /// <summary> /// 创建VLC播放资源索引 /// </summary> /// <param name="arguments"></param> /// <returns></returns> private libvlc_instance_t Create_Media_Instance() { libvlc_instance_t libvlc_instance = IntPtr.Zero; IntPtr argvPtr = IntPtr.Zero; try { if (arguments.Length == 0 || arguments == null) { return IntPtr.Zero; } //将string数组转换为指针 argvPtr = StrToIntPtr(arguments); if (argvPtr == null || argvPtr == IntPtr.Zero) { return IntPtr.Zero; } //设置启动参数 libvlc_instance = SafeNativeMethods.libvlc_new(arguments.Length, argvPtr); if (libvlc_instance == null || libvlc_instance == IntPtr.Zero) { return IntPtr.Zero; } return libvlc_instance; } catch { return IntPtr.Zero; } } } #region vlclib.dll internal static class LibVlcAPI { internal struct PointerToArrayOfPointerHelper { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)] public IntPtr[] pointers; } /// <summary> /// 传入播放参数 /// </summary> /// <param name="arguments"></param> /// <returns></returns> 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) 从进程的非托管内存中分配的内存。 */ } } } /// <summary> /// 从本地文件系统路径新建,其他参照上一条 /// </summary> /// <param name="libvlc_instance"></param> /// <param name="path"></param> /// <returns></returns> 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) 从进程的非托管内存中分配的内存。 */ } } } /// <summary> /// 使用一个给定的媒体资源路径来建立一个libvlc_media对象.参数psz_mrl为要读取的MRL(Media Resource Location).此函数返回新建的对象或NULL. /// </summary> /// <param name="libvlc_instance"></param> /// <param name="path"></param> /// <returns></returns> 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导出函数 /// <summary> /// 创建一个libvlc实例,它是引用计数的 /// </summary> /// <param name="argc"></param> /// <param name="argv"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] private static extern IntPtr libvlc_new(int argc, IntPtr argv); /// <summary> /// 释放libvlc实例 /// </summary> /// <param name="libvlc_instance"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_release(IntPtr libvlc_instance); /// <summary> /// 获取版本 /// </summary> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern String libvlc_get_version(); /// <summary> /// 从视频来源(例如Url)构建一个libvlc_meida /// </summary> /// <param name="libvlc_instance"></param> /// <param name="path"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path); /// <summary> /// 从本地文件路径构建一个libvlc_media /// </summary> /// <param name="libvlc_instance"></param> /// <param name="path"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path); /// <summary> /// /// </summary> /// <param name="libvlc_media_inst"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_release(IntPtr libvlc_media_inst); /// <summary> /// 创建libvlc_media_player(播放核心) /// </summary> /// <param name="libvlc_instance"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance); /// <summary> /// 将视频(libvlc_media)绑定到播放器上 /// </summary> /// <param name="libvlc_media_player"></param> /// <param name="libvlc_media"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media); /// <summary> /// 设置图像输出的窗口 /// </summary> /// <param name="libvlc_mediaplayer"></param> /// <param name="drawable"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable); #region 播放控制 /// <summary> /// 播放 /// </summary> /// <param name="libvlc_mediaplayer"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer); /// <summary> /// 暂停 /// </summary> /// <param name="libvlc_mediaplayer"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer); /// <summary> /// 停止 /// </summary> /// <param name="libvlc_mediaplayer"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer); #endregion /// <summary> /// 释放播放文件 /// </summary> /// <param name="libvlc_mediaplayer"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer); /// <summary> /// 解析视频资源的媒体信息(如时长等) /// </summary> /// <param name="libvlc_media"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_parse(IntPtr libvlc_media); /// <summary> /// 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效) /// </summary> /// <param name="libvlc_media"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media); #region 播放时间进度 /// <summary> /// 当前播放的时间进度 /// </summary> /// <param name="libvlc_mediaplayer"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer); /// <summary> /// 设置播放位置(拖动) /// </summary> /// <param name="libvlc_mediaplayer"></param> /// <param name="time"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time); #endregion #region 音量 /// <summary> /// 获取音量 /// </summary> /// <param name="libvlc_media_player"></param> /// <returns></returns> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player); /// <summary> /// 设置音量 /// </summary> /// <param name="libvlc_media_player"></param> /// <param name="volume"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume); #endregion /// <summary> /// 设置全屏 /// </summary> /// <param name="libvlc_media_player"></param> /// <param name="isFullScreen"></param> [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [SuppressUnmanagedCodeSecurity] public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen); /// <summary> /// 获取播放状态。(Win10 不支持) /// </summary> /// <param name="libvlc_mediaplayer"></param> /// <returns></returns> //[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] //[SuppressUnmanagedCodeSecurity] //public static extern Int64 libvlc_media_player_get_state(IntPtr libvlc_mediaplayer); } #endregion }
2、新建一个form窗体,在窗体上拖一个panel1控件,然后输入下面代码即可
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinVlcProject { public partial class Form2 : Form { VlcPlayerBase player = null; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { try { string p = System.Environment.CurrentDirectory + "\\plugins\\"; player = new VlcPlayerBase(p); player.SetRenderWindow((int)panel1.Handle); VlcPlayerBase v = new VlcPlayerBase(p); v.playUrl("http://222.128.103.58:21909/system-demo/testvideo.mpg", this.panel1.Handle);// 网络路径播放 这里的这个panel是winform窗体的一个panel的控件 // v.Aspect("1:1");//这个是设置屏幕比的` this.player.SetFullScreen(true); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;//隐藏边框 this.WindowState = System.Windows.Forms.FormWindowState.Maximized; //实现窗体全屏显示 } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
方法方法二(WPF实现):
1、先安装VLC media player3.0,将安装的VLC文件夹整体复制到项目根目录下:
2、然后界面添加VlcControl控件,如下图,代码如下:
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <vlc:VlcControl x:Name="vlcControl"/> </Grid> </Window>
3.然后进入窗体.cs文件,代码如下:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using WpfApp1.Utils; namespace WpfApp1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private readonly IniUtil iniUtil = new IniUtil(AppDomain.CurrentDomain.BaseDirectory + "\\config.ini"); //获取输出目录 private static string appPath = AppDomain.CurrentDomain.BaseDirectory; //vlc文件的目录 private DirectoryInfo vlcLibDirectory = new DirectoryInfo(appPath + @"VLC"); //配置项 string[] options = new string[] { }; public MainWindow() { // Width = iniUtil.getInt("FormSetting", "Width", 0); InitializeComponent(); #region 设置界面全屏显示 this.WindowState = System.Windows.WindowState.Normal; this.WindowStyle = System.Windows.WindowStyle.None; this.ResizeMode = System.Windows.ResizeMode.NoResize; this.Topmost = true; this.Left = 0.0; this.Top = 0.0; this.Width = System.Windows.SystemParameters.PrimaryScreenWidth; this.Height = System.Windows.SystemParameters.PrimaryScreenHeight; #endregion //创建播放器 this.vlcControl.SourceProvider.CreatePlayer(vlcLibDirectory, options); //http协议视频流 this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("http://222.128.103.58:21909/system-demo/testvideo.mpg")); LogHelper.WriteLog(GetType(), "MainWindow播放视频"); //rtmp协议 //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("rtmp://58.200.131.2:1935/livetv/hunantv")); //本地视频 // this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("C:\\Users\\Administrator\\Desktop\\新建文件夹 (4)\\QQ录屏20230103141518.mp4")); //添加播放结束事件 this.vlcControl.SourceProvider.MediaPlayer.EndReached += MediaPlayerEndEvent; } //添加播放结束事件 private void MediaPlayerEndEvent(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e) { LogHelper.WriteLog(GetType(), "进入MediaPlayerEndEvent方法"); Task.Factory.StartNew(() => { this.vlcControl.Dispose(); Application.Current.Dispatcher.BeginInvoke(new Action(() => { this.vlcControl.SourceProvider.CreatePlayer(vlcLibDirectory); this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("http://222.128.103.58:21909/system-demo/testvideo.mpg"));//http协议视频流 LogHelper.WriteLog(GetType(), "MediaPlayerEndEvent重新加载视频"); //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("C:\\Users\\Administrator\\Desktop\\新建文件夹 (4)\\QQ录屏20230103141518.mp4"));//本地视频 this.vlcControl.SourceProvider.MediaPlayer.EndReached += MediaPlayerEndEvent; })); }); } } }
WPF参考地址:https://www.cnblogs.com/rsj1767/p/11955600.html
Winform参考地址:https://blog.csdn.net/qq_38826949/article/details/98749193?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-98749193-blog-127024637.pc_relevant_3mothn_strategy_and_data_recovery&spm=1001.2101.3001.4242.1&utm_relevant_index=3