WPF 用Vlc.DotNet.Wpf实现视频播放、停止、暂停功能

发布于:2024-12-19 ⋅ 阅读:(13) ⋅ 点赞:(0)

1.  NuGet 添加 Vlc.DotNet.Wpf 

2.  到VLC官网 http://www.videolan.org/ 下载VLC播放器。因为本机是64位的,所以下载64位,如下所示:

下载的是 安装后,到安装的路径下,拷贝如下文件夹:

在wpf项目的exe文件夹下,本机是  bin\Debug\net8.0-windows,新建 libvlc 后,再建win-x64文件夹,将以上的文件拷贝到这个文件夹里,如下图所示:

3. 现在进入开发:

3.1 VlcControl 控件有个特别的地方是:

 private void Pause_OnClick(object sender, RoutedEventArgs e)
 {
     _vlcMediaPlayer.Pause();
 }

即使 暂停 按钮的事件里,只有一行暂停代码,但是,点一下按钮会暂停,再点击一下会继续播放,暂停--播放--暂停--播放,反复循环。

 private void Pause_OnClick(object sender, RoutedEventArgs e)
 {
     _vlcMediaPlayer.SetPause(true);
 }

SetPause(true),无论点击多少次,都会暂停。

3.2 VlcControl 控件,stop后,控件会显示黑色,为了不显示黑色,就得显示第一针的图像。

一个VlcControl控件,3个按钮,播放、停止和暂停。

.xaml 代码如下:

  <Grid >
      <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          <RowDefinition Height="40" />
          <!-- <RowDefinition Height="40" /> -->
      </Grid.RowDefinitions>
      <vlc:VlcControl Name="VlcControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
      <StackPanel  Grid.Row="1" Orientation="Horizontal">
      <Button Content="播放" Click="Play_OnClick"></Button>
      <Button   Content="停止" Click="Stop_OnClick"  Width="80"></Button>
      <Button   Content="暂停"  Click="Pause_OnClick"  Width="80"></Button>
      </StackPanel>
  </Grid>

.cs代码如下:

    public partial class MainWindow : Window
    {
        private VlcMediaPlayer _vlcMediaPlayer;
        private DirectoryInfo libDirectory;
        public MainWindow()
        {
            InitializeComponent();
            libDirectory = new DirectoryInfo(System.IO.Path.Combine(Environment.CurrentDirectory, "libvlc", "win-x64"));
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (_vlcMediaPlayer == null)
            {
                var options = new string[]
                {
                    "--file-logging", "-vvv", "--logfile=Logs.log"
                };
                this.VlcControl.SourceProvider.CreatePlayer(libDirectory, options);
                _vlcMediaPlayer = this.VlcControl.SourceProvider.MediaPlayer;

                // 加载视频文件
                _vlcMediaPlayer.SetMedia(new Uri(@"E:\base_pup_2024-11-08_16-50-59\MiceVideo1\MiceVideo\CHA2024-11-08 165102.avi", UriKind.RelativeOrAbsolute));

                // 设置到第一帧并暂停
                // 开始播放
                _vlcMediaPlayer.Play();
                // 设置到视频开始位置
                _vlcMediaPlayer.Position = 0;
                // 暂停,保留第一帧画面
                _vlcMediaPlayer.SetPause(true);
            }
        }

        private void PlayOrPauseButton_Checked(object sender, RoutedEventArgs e)
        {
            this.Play();
        }

        private void PlayOrPauseButton_Unchecked(object sender, RoutedEventArgs e)
        {
            
        }

        private void Play()
        {
            _vlcMediaPlayer.Play();
        }

        private void Stop_OnClick(object sender, RoutedEventArgs e)
        {
            if (_vlcMediaPlayer == null)
            {
                return;
            }

            if (_vlcMediaPlayer.State == MediaStates.Stopped)
            {
                return;
            }
           
            if (_vlcMediaPlayer.State == MediaStates.Playing)
            {
                //先暂停
                Task.Delay(100).ContinueWith(_ => _vlcMediaPlayer.SetPause(true));
            }
            //回到第一帧
            _vlcMediaPlayer.Position = 0;
        }

        private void Pause_OnClick(object sender, RoutedEventArgs e)
        {
            if (_vlcMediaPlayer == null)
            {
                return;
            }
            // _vlcMediaPlayer.Pause();点击按钮,会暂停--播放--暂停--播放,反复循环
            _vlcMediaPlayer.SetPause(true);
        }

        private void Play_OnClick(object sender, RoutedEventArgs e)
        {
            Play();
        }


    }


网站公告

今日签到

点亮在社区的每一天
去签到