WPF调用VLC的方法讲解的很多,可参考[RTSP]WPF用VLC显示RTSP视频。值得注意的是采用页面端创建VlcControl的方式,后端更改VlcVideoSourceProvider. MediaPlayer.Play的options时没有效果,如果设置VlcVideoSourceProvider.MediaPlayer.Video.AspectRatio改变宽高比也没有效果,不知道是不是更改方式有问题(成功解决的可以留言交流下)。所以就换一个加载思路:在页面端使用Image控件来展示,后端采用Image绑定VlcVideoSourceProvider.VideoSource的方式加载,只需要设置Image控件的Stretch="Fill"即可实现视频比例的缩放。
实现效果:
1、首先在工程目录下新建一个文件夹如libvlc里面放x64和x86的VLC相关插件
2、窗体
3、交互逻辑,开了4个线程去分别加载视频流,LogHelper类参考C#一个完整的Log4net使用实例
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using VideoMonitoring.Utils;
using Vlc.DotNet.Wpf;
namespace VideoMonitoring
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
private VlcVideoSourceProvider sourceProvider1;
private VlcVideoSourceProvider sourceProvider2;
private VlcVideoSourceProvider sourceProvider3;
private VlcVideoSourceProvider sourceProvider4;
private string RTSPPath = "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";//视频地址
public MainWindow()
{
InitializeComponent();
}
///
/// 页面加载
///
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//加载视频
TaskFactory tf = new TaskFactory();
Task taskTrack1 = tf.StartNew(() => LoadVideo1());
Task taskTrack2 = tf.StartNew(() => LoadVideo2());
Task taskTrack3 = tf.StartNew(() => LoadVideo3());
Task taskTrack4 = tf.StartNew(() => LoadVideo4());
}
#region 视频加载
private void LoadVideo1()
{
try
{
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "x86" : "x64"));
sourceProvider1 = new VlcVideoSourceProvider(this.Dispatcher);
sourceProvider1.CreatePlayer(libDirectory);
sourceProvider1.MediaPlayer.Play(new Uri(RTSPPath));//可替换为视频监控地址
this.Video1.Dispatcher.Invoke(() => {
this.Video1.SetBinding(System.Windows.Controls.Image.SourceProperty,
new Binding(nameof(VlcVideoSourceProvider.VideoSource)) { Source = sourceProvider1 });
});
}
catch (Exception ex)
{
LogHelper.log.Error(string.Format("加载视频1失败:{0}", ex));
}
}
private void LoadVideo2()
{
try
{
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "x86" : "x64"));
sourceProvider2 = new VlcVideoSourceProvider(this.Dispatcher);
sourceProvider2.CreatePlayer(libDirectory);
sourceProvider2.MediaPlayer.Play(new Uri(RTSPPath));
this.Video2.Dispatcher.Invoke(() => {
this.Video2.SetBinding(System.Windows.Controls.Image.SourceProperty,
new Binding(nameof(VlcVideoSourceProvider.VideoSource)) { Source = sourceProvider2 });
});
}
catch (Exception ex)
{
LogHelper.log.Error(string.Format("加载视频2失败:{0}", ex));
}
}
private void LoadVideo3()
{
try
{
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "x86" : "x64"));
sourceProvider3 = new VlcVideoSourceProvider(this.Dispatcher);
sourceProvider3.CreatePlayer(libDirectory);
sourceProvider3.MediaPlayer.Play(new Uri(RTSPPath));
this.Video3.Dispatcher.Invoke(() => {
this.Video3.SetBinding(System.Windows.Controls.Image.SourceProperty,
new Binding(nameof(VlcVideoSourceProvider.VideoSource)) { Source = sourceProvider3 });
});
}
catch (Exception ex)
{
LogHelper.log.Error(string.Format("加载视频3失败:{0}", ex));
}
}
private void LoadVideo4()
{
try
{
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "x86" : "x64"));
sourceProvider4 = new VlcVideoSourceProvider(this.Dispatcher);
sourceProvider4.CreatePlayer(libDirectory);
sourceProvider4.MediaPlayer.Play(new Uri(RTSPPath));
this.Video4.Dispatcher.Invoke(() => {
this.Video4.SetBinding(System.Windows.Controls.Image.SourceProperty,
new Binding(nameof(VlcVideoSourceProvider.VideoSource)) { Source = sourceProvider4 });
});
}
catch (Exception ex)
{
LogHelper.log.Error(string.Format("加载视频4失败:{0}", ex));
}
}
#endregion
private void Window_Closed(object sender, EventArgs e)
{
if (sourceProvider1 != null)
{
sourceProvider1.Dispose();
}
if (sourceProvider2 != null)
{
sourceProvider2.Dispose();
}
if (sourceProvider3 != null)
{
sourceProvider3.Dispose();
}
if (sourceProvider4 != null)
{
sourceProvider4.Dispose();
}
}
}
}