最近公司需要对接大华视频的webapi,获取的实时视频是rtsp流的,最后经过不断尝试和查询博客最终还是发现使用VLC进行播放比较好
一、nuget引用VLC资源包
打开项目的nuget进行对于VLC的引用,成功后显示的是这四个包
二、设计视频显示界面
整体主要显示的是 视频,然后最下方鼠标移入视频后会显示一些操作按钮和进度条,最终样式如下图所示:
按钮使用的是icon,进度条使用的是Slider,都是使用的一个开源框架HandyControl 里边的样式,nuget里边就可以直接饮用,文档:https://gitee.com/handyorg/HandyControl 还有https://handyorg.gitee.io/handycontrol/
三、使用VLC自带控件播放rtsp视频流
1、下载VLC播放器并提取其文件
在使用VLC之前 我们需要安装一下VLC media player 下载地址:https://vlc-media-player.en.softonic.com/
下载安装后我们打开安装路径,提取其路径中的plugins文件夹和libvlc.dll、libvlccore.dll,并将这三个放入至我们项目的debug中同一个文件夹内,取名文件夹为 LibVlc(文件夹名字可以随意,但是代码中要替换掉)
2、在程序中加载资源,并且使用VLC视频播放控件
(1)、前台直接使用的方式:
引入VLC的命名空间
xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
设计中使用
<ContentControl x:Name="contentCtrl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<wpf:VlcControl/>
</ContentControl>
(2)、后台代码中使用
VlcControl vlcVideo = null;
private void InitVLC()
{
if (this.vlcVideo?.SourceProvider?.MediaPlayer != null)
{
this.vlcVideo.SourceProvider.MediaPlayer.PositionChanged -= MediaPlayer_PositionChanged;
this.vlcVideo.SourceProvider.MediaPlayer.LengthChanged -= MediaPlayer_LengthChanged;
}
this.vlcVideo = new VlcControl();
this.contentCtrl.Content = this.vlcVideo;
var libDirectory = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory() + "\\LibVlc");
this.vlcVideo.SourceProvider.CreatePlayer(libDirectory);//创建视频播放器
this.vlcVideo.SourceProvider.MediaPlayer.PositionChanged += MediaPlayer_PositionChanged;//视频的定位移动事件
this.vlcVideo.SourceProvider.MediaPlayer.LengthChanged += MediaPlayer_LengthChanged;//播放视频源的视频长度
}
(3)、注意事项
两种方法都需要给VLCVideo加载资源文件夹,也就是咱们 三.1 中的那个文件夹,代码就是:
var libDirectory = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory() + "\\LibVlc");
this.vlcVideo.SourceProvider.CreatePlayer(libDirectory);//创建视频播放器
播放:
string videourl=urlobj["uri"];
this.vlcVideo.SourceProvider.MediaPlayer.Play(new Uri(videourl));
暂停:
vlcVideo.SourceProvider.MediaPlayer.Pause();
四、源代码地址
如果帮助了你请点个赞