前言
本文主要讲述,在WPF中,借助Vlc.DotNet调用VLC类库,实现视频播功能,下面我们先来做开发前的准备工作。
准备工作
首先,我们创建一个项目WpfVLC,然后,进入Neget搜索Vlc.DotNet,得到如下界面:

我们选择Vlc.DotNet.Wpf,点击安装(这里我已经安装了,所以图中显示为卸载)。
然后,我们去VLC官网,下载VLC播放器。
VLC官网: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的命名空间。
| 1 | xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf" | 
接着,我们在页面布局中加入VlcControl控件和打开文件、播放、停止的按钮,如下:
| 1 2 3 4 5 6 7 8 | <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文件的代码,如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | publicpartialclassMainWindow : Window{    privatestringfilePath;    publicMainWindow()    {        InitializeComponent();    }     privatevoidWindow_Loaded(objectsender, RoutedEventArgs e)    {        varcurrentAssembly = Assembly.GetEntryAssembly();        varcurrentDirectory = newFileInfo(currentAssembly.Location).DirectoryName;        varlibDirectory = newDirectoryInfo(System.IO.Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86": "win-x64"));        this.VlcControl.SourceProvider.CreatePlayer(libDirectory);     }    privatevoidopen_Click(objectsender, RoutedEventArgs e)    {        OpenFileDialog ofd = newOpenFileDialog();        ofd.Multiselect = false;        ofd.Title = "请选择视频文件";         varresult = ofd.ShowDialog();        if(result == System.Windows.Forms.DialogResult.OK)        {            filePath = ofd.FileName;            try            {                btnPause.Content = "暂停";                this.VlcControl.SourceProvider.MediaPlayer.Play(newUri(filePath));            }            catch(Exception ex)            {                            }        }     }    publicvoidpause_Click(objectsender, RoutedEventArgs e)    {        if(btnPause.Content.ToString() == "播放")        {            btnPause.Content = "暂停";            this.VlcControl.SourceProvider.MediaPlayer.Play();        }        else        {            btnPause.Content = "播放";            this.VlcControl.SourceProvider.MediaPlayer.Pause();        }    }    privatevoidstop_Click(objectsender, RoutedEventArgs e)    {        newTask(() =>        {            this.VlcControl.SourceProvider.MediaPlayer.Stop();//这里要开线程处理,不然会阻塞播放        }).Start();    }} | 
这样,我们就完成了最基本的视频播放、暂停、停止的功能。
可以看到,播放、暂停、停止的代码非常简单,就是调用控件的play,pause,stop函数即可。
因为VLC非常优秀,可以支持多种格式的文件播放,所以我们写的这个播放器也就可以打开任意类型的视频文件。
播放界面如下:

现在,加入Slider控制播放进度和音量。
Slider样式,参考如下文章:
VlcControl控制播放进度的方法很简单,如下:
| 1 2 3 4 5 6 7 8 9 | privatevoidSlider1_DragCompleted(objectsender, System.Windows.Controls.Primitives.DragCompletedEventArgs e){     varposition = (float)(slider1.Value / slider1.Maximum);    if(position == 1)    {        position = 0.99f;    }    this.VlcControl.SourceProvider.MediaPlayer.Position = position;//Position为百分比,要小于1,等于1会停止} | 
控制播放声音的方法如下:
| 1 2 3 4 5 | privatevoidSlider2_DragCompleted(objectsender, 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的地址即可,如下:
| 1 | this.VlcControl.SourceProvider.MediaPlayer.Play(newUri(rtsp://192.168.1.111)); | 
播放摄像头
播放摄像头在这里也很简单,只是Play的入参稍微要注意一下即可,如下:
| 1 2 3 4 5 | stringmrl = @"dshow://  ";stringoptVideo = @":dshow-vdev=摄像头设备名";//string optAudio = @":dshow-adev=音频设备名";stringsize = ":dshow-size=800";this.VlcControl.SourceProvider.MediaPlayer.Play(mrl, optVideo, size); | 
----------------------------------------------------------------------------------------------------
到此C#开发可播放摄像头及任意格式视频的播放器完成了。
代码已经传到Github上了,欢迎大家下载。
Github地址:https://github.com/kiba518/WpfVLC
----------------------------------------------------------------------------------------------------
注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的【推荐】,非常感谢!
https://www.cnblogs.com/kiba/p/11303137.html
 
原文:https://www.cnblogs.com/lonelyxmas/p/11645173.html