C# WPF VLCLib绑定image控件源执行Stop导致死锁的解决

在WPF中,使用Vlc.DotNet.Wpf时,我们可以使用绑定Image控件源的办法实现媒体的播放。

xaml:

……
<Image x:Name="imageMonitor" Margin="5" />
……

代码:

……
VlcVideoSourceProvider sourceProvider = new VlcVideoSourceProvider(this.Dispatcher);
var vlcLibDirectory = new DirectoryInfo(vlcPath); //VLC目录
sourceProvider.CreatePlayer(vlcLibDirectory);
//绑定到image
this.imageMonitor.Dispatcher.Invoke(() =>
{                   
    this.imageMonitor.SetBinding(System.Windows.Controls.Image.SourceProperty,
    new Binding(nameof(VlcVideoSourceProvider.VideoSource)) { Source = sourceProvider });
});

sourceProvider.MediaPlayer.Play(new Uri("screen://"), playOption);

……

需要停止时,如果直接执行:sourceProvider.MediaPlayer.Stop();会导致程序死锁。这是由于线程冲突造成的,解决方法有如下几个:

1、在别的线程中执行停止:

ThreadPool.QueueUserWorkItem(_ => sourceProvider1.MediaPlayer.Stop());

2、sourceProvider.MediaPlayer.Pause();//暂停后,可以直接播放其它内容

3、sourceProvider.MediaPlayer.Play(“”);//画面停在最后一帧,类似暂停

4、sourceProvider.Dispose();//销毁对象,再次播放需要重新实例化vlc对象

猜你喜欢

转载自blog.csdn.net/dgnankai/article/details/129059210