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_; /// <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 != )
{
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>
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 * ));
} /// <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 ? : );
} /// <summary>
/// 视频时长
/// </summary>
/// <returns></returns>
public double Duration { get { return duration_; } } /// <summary>
/// 是否正在播放
/// </summary>
public bool IsPlaying
{
get
{
if (Duration > && (int)GetPlayTime() == (int)Duration) this.Stop(); //如果播放完,关闭视频 return (int)GetPlayTime() < (int)Duration /* 播放时间进度小于视频时长 */
&& Duration > /* 播放时间进度大于0 */
&& GetPlayTime() > ; /* 视频时长大于0 */
}
} /// <summary>
/// 获取版本(VS2015 调试模式程序会直接崩掉)
/// </summary>
/// <returns></returns>
public string Version { get { return LibVlcAPI.libvlc_get_version(); } } } #region vlclib.dll internal static class LibVlcAPI
{
internal struct PointerToArrayOfPointerHelper
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
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[]; for (int i = ; 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 = ; i < arguments.Length + ; 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 + );
Marshal.Copy(bytes, , pMrl, bytes.Length);
Marshal.WriteByte(pMrl, bytes.Length, );
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 + );
Marshal.Copy(bytes, , pMrl, bytes.Length);
Marshal.WriteByte(pMrl, bytes.Length, );
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 }

第三步:写代码实现

string pluginPath = Environment.CurrentDirectory + "\\plugins\\";  //插件目录
player = new VlcPlayerBase(pluginPath);
player.SetRenderWindow((int)pnlVideo.Handle);//panel
player.LoadFile(videoName[]);//视频文件路径

设置音量:

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);

c# winform vlcPlayer播放器的更多相关文章

  1. WinForm媒体播放器

    媒体播放控件(Windows Media Player )的常用属性和方法,并且利用它设计一个简单的媒体应用程序——媒体播放器.该媒体播放器可以播放 wav.avi.mid 和 mp3 等格式的文件. ...

  2. c# winform DirectX播放器 可以任意设置宽高比 屏幕拉伸

    第一步:dll引用 Microsoft.DirectX.dll Microsoft.DirectX.AudioVideoPlayback.dll 如果没有的话,可能需要安装微软的DRECTX JDK ...

  3. winform音频播放器(有声小说[凡人修仙传])

    该程序采用多线程的技术及DataGridView单元格扩展的技术 1.获取下载列表 private void GetDownList() { //System.Web.HttpUtility.UrlD ...

  4. 小菜学习Winform(二)WMPLib实现音乐播放器

    前言 现在网上有很多的音乐播放器,但好像都不是.net平台做的,在.net中实现音乐文件的播放功能很简单,下面就简单实现下. SoundPlayer类 在.net提供了音乐文件的类:SoundPlay ...

  5. winform下的简易播放器

    编写这个播放器,遇到很多问题,比如目前只实现了wav音频文件的播放,而对于这个图中中间所标注的按钮 不能实现让其暂停的功能,同时当点击的时候,让其文本变为"▷",对于这部分功能不知 ...

  6. C# VLCPlayer视频播放器(附源码)

    VLCPlayer视频播放器. 支持本地播放,支持网络URL.支持全屏,截图. 基于VLCPlayer. 附带基本代码. 下载地址:http://pan.baidu.com/s/1nvjNvID

  7. .NET中使用APlayer组件自制播放器

    目录 说明 APlayer介绍 APlayer具备功能 APlayer使用 自制播放器Demo 未完成工作 源码下载 说明 由于需求原因,需要在项目中(桌面程序)集成一个在线播放视频的功能.大概要具备 ...

  8. AR播放器

    一.项目需求 AR播放器:将一系列带透明通道的图片以一定的帧率连续显示,叠加载摄像头采集的画面之上,并播放声音. 此为最初级的AR技术,因为画面是事先渲染好的,固定不变的,所以实际上并不能实现“互动” ...

  9. 在Winform中播放视频等【DotNet,C#】

    在项目中遇到过这样的问题,就是如何在Winform中播放视频.当时考察了几种方式,第一种是直接使用Windows Media Player组件,这种最简单:第二种是利用DirectX直接在窗体或者控件 ...

随机推荐

  1. Delphi字符串简码

    从网上找的三个函数自己修改了下,在delphi7运行正常,unicode的版本不能用好像 输入:1abc天天 输出:1ABCTT unit UnitJM; interface uses SysUtil ...

  2. 通过IP地址屏蔽各种“推广”

    事情的起因是这样的:最近老是发现iPhone应用的底部出现各种横条广告,一开始以为是Google的广告推广,所以没管它,但是最近这些广告越来越猖狂,里面的内容越来越垃圾.今天仔细一看,原来不是Goog ...

  3. GCC编译C代码

    C程序的编译过程       常用文件的后缀名: gcc编译c代码 1.gcc 常用编译选项: 2.gcc编译方法 testc.c: #include <stdio.h> int main ...

  4. PXE DHCP获取IP与传统DHCP获取IP地址的区别

    正常的DHCP获取IP的流程(Discover-Offer-Request-Ack): (Discovery)主机端在LAN中发布MAC地址为FF:FF:FF:FF:FF:FF的广播来寻找DHCP服务 ...

  5. Angular-ngtable联动全选

    之前于Angular第三方插件ngTable的官网demo上看到的例子,但苦于demo中联动全选为选中所有,项目中并不适用,因此做了下小小的修改,修改目的只是为实现其功能,方法不敢苟同,若有更加简便的 ...

  6. 在没有安装有mvc3的主机上部署asp.net mvc3网站,需要包含的DLL文件

    原文:在没有安装有mvc3的主机上部署asp.net mvc3网站,需要包含的DLL文件 http://hi.baidu.com/aspxdiyer/blog/item/5515a69943232f1 ...

  7. visual studio2015使用git管理源代码

    1.注册https://git.oschina.net/ 2.注册好后,创建一个测试项目,如下图: 点击创建,如下: 上面的红框中的地址下面会用到. 3.git初始化设置 在本地电脑要安装git,打开 ...

  8. apche配置后报错(Forbidden)没有权限

    apche如何配置虚拟目录及虚拟目录的权限 <Directory /> Options FollowSymLinks AllowOverride None Order deny,allow ...

  9. 用jsonp格式的数据进行ajax post请求变成get

    因为 dataType 是 jsonp 而不是 json jsonp不支持POST跨域,所以会自动转成GET而关于jsonp为什么不支持post请求,百度到的答案是jsonp为动态的script,没有 ...

  10. ssh无密码通信设置

    ■单向登陆配置:1.在本地机器中的~/.ssh/目录下执行下命令#ssh-keygen -t dsa然后全部回车,采用默认值.生成了一对密钥,id_dsa和id_dsa.pub,存放在用户目录的~/. ...