WPF 使用LibVLCSharp.WPF实现视频播放、停止、暂停功能

使用LibVLCSharp.WPF实现视频播放、停止、暂停功能

1, NuGet 添加 VideoLAN.LibVLC.Windows

  1. NuGet 添加 LibVLCSharp.WPF

  2. wpf 代码如下:

XML 复制代码
 <Grid >
     <Grid.RowDefinitions>
         <RowDefinition Height="*" />
         <RowDefinition Height="40" />
         <!-- <RowDefinition Height="40" /> -->
     </Grid.RowDefinitions>
     <wpf:VideoView 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>
  1. .cs代码如下:
cs 复制代码
    public partial class MainWindow : Window
    {
        private LibVLC _libVLC;
        private MediaPlayer _mediaPlayer;
        public MainWindow()
        {
            InitializeComponent();

            Core.Initialize();// 初始化 LibVLC
            _libVLC = new LibVLC();
            _mediaPlayer = new MediaPlayer(_libVLC);
            VlcControl.MediaPlayer = _mediaPlayer;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //取整设置成刚好的网格大小
          
        }

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

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

        private void Play()
        {
            //调用Play()时,MediaElement会将其Position设置为0
            if (_mediaPlayer.State == VLCState.Paused)
            {
                var oldTime = _mediaPlayer.Time;
                _mediaPlayer.Play();
                _mediaPlayer.Time = oldTime;
            }
            else
            {
                string filePath = @"E:\1.avi";
                _mediaPlayer.Play(new Media(_libVLC, new Uri(filePath, UriKind.RelativeOrAbsolute)));
            }

        }

       private void Stop_OnClick(object sender, RoutedEventArgs e)
       {
            _mediaPlayer.Stop();
       }

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

       private void Play_OnClick(object sender, RoutedEventArgs e)
      {
            Play();
      }
    }
相关推荐
dotent·14 小时前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
咩图2 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
雁于飞2 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii2 天前
WPF入门指南:解析默认项目结构
wpf
极客智造2 天前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Macbethad4 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168884 天前
WPF Prism框架应用
c#·wpf·prism
Macbethad4 天前
使用WPF编写一个Ethercat主站的程序
wpf
难搞靓仔4 天前
WPF 弹出窗体Popup
wpf·popup
Macbethad4 天前
使用WPF编写一个MODBUSTCP通信的程序
wpf