之前写过一篇文章关于WPF利用VLCPlayer控制Winform窗体句柄封装的视频播放器(链接:https://blog.csdn.net/dnazhd/article/details/102476134),这里换一种方式重写一下视频播放器控件,采用VlcVideoSourceProvider绑定Image控件。
实现步骤:
1、添加对VLC控件的引用
2、UCVlcPlayer窗体xaml
将方式一中的winform控件:
修改为:
3、交互逻辑
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Vlc.DotNet.Wpf;
namespace VlcPlayerWPFDemo
{
///
/// UCVlcPlayer.xaml 的交互逻辑
///
public partial class UCVlcPlayer : UserControl
{
private volatile bool m_isDrag;
private string filePath = "";
private double acWidth = 600;
private double acHeight = 440;
private double fullWidth = 600;
private double fullHeight = 440;
private VlcVideoSourceProvider sourceProvider;
public delegate void SetFullScreenEvent(bool isFull);
public event SetFullScreenEvent SetFullScreen;
public UCVlcPlayer()
{
InitializeComponent();
CreateMedia();
}
///
/// 创建媒体资源
///
private void CreateMedia()
{
try
{
var libDirectory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
sourceProvider = new VlcVideoSourceProvider(this.Dispatcher);
sourceProvider.CreatePlayer(libDirectory);
sourceProvider.MediaPlayer.PositionChanged += MediaPlayer_PositionChanged;
sourceProvider.MediaPlayer.EndReached += MediaPlayer_EndReached;
sourceProvider.MediaPlayer.TimeChanged += MediaPlayer_TimeChanged;
sourceProvider.MediaPlayer.LengthChanged += MediaPlayer_LengthChanged;
volumeSlider.Value = sourceProvider.MediaPlayer.Audio.Volume;
}
catch (Exception ex)
{
}
}
///
/// 释放资源
///
public void DisposePlayer()
{
try
{
if (sourceProvider != null)
{
sourceProvider.Dispose();
sourceProvider = null;
}
}
catch (Exception ex)
{
}
}
///
/// 设置原始宽高
///
public void InitSize(double width, double height)
{
acWidth = width;
acHeight = height;
}
///
/// 设置最大化以后宽高
///
public void InitFullSize(double width, double height)
{
fullWidth = width;
fullHeight = height;
}
public void OpenPlayer(string path = "")
{
if(!string.IsNullOrEmpty(path)) filePath = path;
if (sourceProvider.MediaPlayer.State == Vlc.DotNet.Core.Interops.Signatures.MediaStates.NothingSpecial || sourceProvider.MediaPlayer.State == Vlc.DotNet.Core.Interops.Signatures.MediaStates.Ended)
{
if (File.Exists(filePath))
{
sourceProvider.MediaPlayer.Play(new FileInfo(filePath));
this.VideoImg.Dispatcher.Invoke(() =>
{
this.VideoImg.SetBinding(System.Windows.Controls.Image.SourceProperty, new Binding(nameof(VlcVideoSourceProvider.VideoSource)) { Source = sourceProvider });
});
}
}
else
{
sourceProvider.MediaPlayer.Play();
}
}
#region 绑定事件
private void InitControls()
{
this.VideoImg.Source = null;
playProgressSlider.Value = 0;
lblCurTime.Content = "00:00:00";
if (this.imgPlay.Tag.ToString() != "play")
{
this.imgPlay.Tag = "play";
this.imgPlay.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/Play.png"));
}
if (sourceProvider.MediaPlayer != null) sourceProvider.MediaPlayer.Rate = 1;
}
private void MediaPlayer_LengthChanged(object sender, Vlc.DotNet.Core.VlcMediaPlayerLengthChangedEventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(delegate
{
lblTotalTime.Content = TimeSpan.FromMilliseconds(e.NewLength).ToString().Substring(0, 8);
}));
}
private void MediaPlayer_TimeChanged(object sender, Vlc.DotNet.Core.VlcMediaPlayerTimeChangedEventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(delegate
{
lblCurTime.Content = TimeSpan.FromMilliseconds(e.NewTime).ToString().Substring(0, 8);
}));
}
private void MediaPlayer_EndReached(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(delegate
{
InitControls();
}));
}
private void MediaPlayer_PositionChanged(object sender, Vlc.DotNet.Core.VlcMediaPlayerPositionChangedEventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(delegate
{
if (!m_isDrag)
{
playProgressSlider.Value = (double)e.NewPosition;
}
}));
}
#endregion
#region 事件
private void volumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
sourceProvider.MediaPlayer.Audio.Volume = (int)e.NewValue;
}
private void playProgressSlider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
sourceProvider.MediaPlayer.Position = (float)playProgressSlider.Value;
m_isDrag = false;
}
private void playProgressSlider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
m_isDrag = true;
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
sourceProvider.MediaPlayer.Rate -= 0.1f;
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
sourceProvider.MediaPlayer.ResetMedia();
InitControls();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
sourceProvider.MediaPlayer.Rate += 0.1f;
}
private void imgPlay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (this.imgPlay.Tag.ToString() == "play")
{
OpenPlayer();
this.imgPlay.Tag = "pause";
this.imgPlay.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/Pause.png"));
}
else
{
sourceProvider.MediaPlayer.Pause();
this.imgPlay.Tag = "play";
this.imgPlay.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/Play.png"));
}
}
private void imgFullScreen_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (this.imgFullScreen.Tag.ToString() == "full")
{
this.imgFullScreen.Tag = "exit";
this.imgFullScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/fullscreen_exit.png"));
this.Width = fullWidth;
this.Height = fullHeight;
SetFullScreen?.Invoke(true);
}
else
{
this.imgFullScreen.Tag = "full";
this.imgFullScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/fullscreen.png"));
this.Width = acWidth;
this.Height = acHeight;
SetFullScreen?.Invoke(false);
}
}
#endregion
}
}
4、运行效果和方式一一样