前言

本文主要讲述,在WPF中,借助Vlc.DotNet调用VLC类库,实现视频播功能,下面我们先来做开发前的准备工作。

准备工作

首先,我们创建一个项目WpfVLC,然后,进入Neget搜索Vlc.DotNet,得到如下界面:



我们选择Vlc.DotNet.Wpf,点击安装(这里我已经安装了,所以图中显示为卸载)。

然后,我们去VLC官网,下载VLC播放器。

VLC官网:http://www.videolan.org/ <http://www.videolan.org/>

因为我的电脑是64位的,所以我下载64位的VLC版本,如下图:



 下载完成后,正常安装即可,下载的文件截图如下:



安装完成后,我们找到安装的具体位置并打开,如下图:



在文件夹内我们找到文件libvlc.dll,libvlccore.dll和文件夹plugins,然后将他们复制出来。

现在我们回到我们刚刚创建的项目WpfVLC,进入文件目录,打开debug文件夹,然后我们在其目录下创建一个文件夹libvlc,如下:



然后,在在liblic下建立一个文件夹win-x64,如下:



再然后,我们将刚刚复制的vlc的三个文件,放到这个文件夹下,如下:



到此,我们的准备工作就完成了,现在开始编码。

使用Vlc.DotNet播放视频

现在,我们进入项目的代码开发。

首先我们将项目设置为64位项目,因为我们使用的VLC是64的。



然后,我们打开MainWindow页面。

在页面命名空间引入的地方加入Vlc.DotNet的命名空间。
xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
接着,我们在页面布局中加入VlcControl控件和打开文件、播放、停止的按钮,如下:
<DockPanel DockPanel.Dock="Bottom"> <StackPanel Height="50"
DockPanel.Dock="Bottom" Orientation="Horizontal"> <Button Name="btnOpen"
Content="打开文件" Click="open_Click" Width="80"></Button> <Button Name="btnPause"
Content="暂停" Click="pause_Click" Width="50"></Button> <Button Name="btnStop"
Content="停止" Click="stop_Click" Width="50"></Button> </StackPanel> </DockPanel>
<vlc:VlcControl x:Name="VlcControl" />
然后,我们编写xaml.cs文件的代码,如下:
public partial class MainWindow : Window { private string filePath; public
MainWindow() { InitializeComponent(); } private void Window_Loaded(object
sender, RoutedEventArgs e) { var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory,
"libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
this.VlcControl.SourceProvider.CreatePlayer(libDirectory); } private void
open_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new
OpenFileDialog(); ofd.Multiselect = false; ofd.Title = "请选择视频文件"; var result =
ofd.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) {
filePath = ofd.FileName; try { btnPause.Content = "暂停";
this.VlcControl.SourceProvider.MediaPlayer.Play(new Uri(filePath)); } catch
(Exception ex) { } } } public void pause_Click(object sender, RoutedEventArgs
e) { if (btnPause.Content.ToString() == "播放") { btnPause.Content = "暂停";
this.VlcControl.SourceProvider.MediaPlayer.Play(); } else { btnPause.Content =
"播放"; this.VlcControl.SourceProvider.MediaPlayer.Pause(); } } private void
stop_Click(object sender, RoutedEventArgs e) { new Task(() => {
this.VlcControl.SourceProvider.MediaPlayer.Stop();//这里要开线程处理,不然会阻塞播放
}).Start(); } }
这样,我们就完成了最基本的视频播放、暂停、停止的功能。

可以看到,播放、暂停、停止的代码非常简单,就是调用控件的play,pause,stop函数即可。

因为VLC非常优秀,可以支持多种格式的文件播放,所以我们写的这个播放器也就可以打开任意类型的视频文件。

播放界面如下:



现在,加入Slider控制播放进度和音量。

Slider样式,参考如下文章:

WPF依赖属性的正确学习方法 <https://www.cnblogs.com/kiba/p/11149147.html>

WPF滑块控件(Slider)的自定义样式 <https://www.cnblogs.com/kiba/p/11253686.html>

VlcControl控制播放进度的方法很简单,如下:
private void Slider1_DragCompleted(object sender,
System.Windows.Controls.Primitives.DragCompletedEventArgs e) { var position =
(float)(slider1.Value / slider1.Maximum); if (position == 1) { position =
0.99f; } this.VlcControl.SourceProvider.MediaPlayer.Position =
position;//Position为百分比,要小于1,等于1会停止 }
控制播放声音的方法如下:
private void Slider2_DragCompleted(object sender,
System.Windows.Controls.Primitives.DragCompletedEventArgs e) {
//Audio.Volume:音量的百分比,值在0—200之间
this.VlcControl.SourceProvider.MediaPlayer.Audio.Volume = (int)slider2.Value; }
这样我们的播放器就开发完成了。

最终界面如下:



播放其他视频源

播放RTSP

通过上面的代码编写,我们了解到了,在C#里使用VLC播放视频的代码非常简单,只要在Play函数中写入地址即可。

那么播放RTSP自然是同理,只要在Play中写入RTSP的地址即可,如下:
this.VlcControl.SourceProvider.MediaPlayer.Play(new
Uri(rtsp://192.168.1.111));
播放摄像头

播放摄像头在这里也很简单,只是Play的入参稍微要注意一下即可,如下:
string mrl = @"dshow:// "; string optVideo = @":dshow-vdev=摄像头设备名"; //string
optAudio = @":dshow-adev=音频设备名"; string size = ":dshow-size=800";
this.VlcControl.SourceProvider.MediaPlayer.Play(mrl, optVideo, size);

----------------------------------------------------------------------------------------------------

到此C#开发可播放摄像头及任意格式视频的播放器完成了。

代码已经传到Github上了,欢迎大家下载。

Github地址:https://github.com/kiba518/WpfVLC <https://github.com/kiba518/WpfVLC>


----------------------------------------------------------------------------------------------------

注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的【推荐】,非常感谢!

https://www.cnblogs.com/kiba/p/11303137.html
<https://www.cnblogs.com/kiba/p/11303137.html>

 

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信