WPF 使用 VLC 3.0.10 的基本操作

 先展示基本效果:

WPF 使用 VLC 3.0.10 的基本操作_第1张图片

实现过程:

首先通过 Nuget 下载 VLC 相关的组件;

WPF 使用 VLC 3.0.10 的基本操作_第2张图片

View 层(XAML)

添加引用 和 控件(里面使用了许多自定义的控件样式,这里就不在赘述)


    
        
            
            
        
        
        
            
                
            
            
                
                
            
            
                
                    

基本的控件加载和视频控制事件:

     #region 初始化
        /// 
        /// 初始化
        /// 
        public uc_video()
        {
            InitializeComponent();
            DataContext = my_vm;

            if (vc.SourceProvider.MediaPlayer == null)
            {
                var vlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
                Task.Factory.StartNew(() =>
                {
                    App.dispatcher.Invoke(() =>
                    {
                        vc.SourceProvider.CreatePlayer(vlcLibDirectory, new string[] { });
                    });
                });
            }
            Init_Timer();
            progressbar.Minimum = 0;
            progressbar.Maximum = 100000;

            my_vm.DropDownList_Rate.Clear();
            my_vm.DropDownList_Rate.Add(new KeyValuePair("慢放", 0.5f));
            my_vm.DropDownList_Rate.Add(new KeyValuePair("正常", 1));
            my_vm.DropDownList_Rate.Add(new KeyValuePair("快放", 2));

            combobox_rate.SelectedIndex = 1;
        }
        #endregion

        #region 控制
        /// 
        /// 加载视频
        /// 
        private void Load_Video()
        {
            if (!string.IsNullOrWhiteSpace(url_video))
            {
                if (vc.SourceProvider.MediaPlayer.State == Vlc.DotNet.Core.Interops.Signatures.MediaStates.Ended)
                {
                    vc.SourceProvider.MediaPlayer.ResetMedia();
                }

                WebRequest req = WebRequest.CreateHttp(url_video);
                try
                {
                    WebResponse res = req.GetResponse();
                    if (res.ContentLength > 0)
                    {
                        vc.SourceProvider.MediaPlayer.Play("您的视频网址或者路径", new string[] { });
                    }
                }
                catch (Exception ex)
                {
                    win_msg.Info("提示", $"该列车视频尚未准备就绪...{ex.Message}"); return;
                }
            }
        }
        /// 
        /// 播放/暂停
        /// 
        /// 
        /// 
        private void chk_Pause_Checked(object sender, RoutedEventArgs e)
        {
            if (vc.SourceProvider.MediaPlayer == null) return;
            if (string.IsNullOrWhiteSpace(url_video)) { win_msg.Info("提示", "该列车视频尚未准备就绪..."); return; }

            if (vc.SourceProvider.MediaPlayer.State == Vlc.DotNet.Core.Interops.Signatures.MediaStates.NothingSpecial ||
            vc.SourceProvider.MediaPlayer.State == Vlc.DotNet.Core.Interops.Signatures.MediaStates.Stopped ||
            vc.SourceProvider.MediaPlayer.State == Vlc.DotNet.Core.Interops.Signatures.MediaStates.Ended)
            {
                Load_Video();
            }

            if (vc.SourceProvider.MediaPlayer.IsPlaying())
            {
                vc.SourceProvider.MediaPlayer.Pause();
                my_vm.IsPaused = true;
            }
            else
            {
                vc.SourceProvider.MediaPlayer.Play();
            }
        }
        /// 
        /// 上一帧
        /// 
        /// 
        /// 
        private void btn_frame_pre(object sender, RoutedEventArgs e)
        {
            if (vc.SourceProvider.MediaPlayer == null) return;
            if (vc.SourceProvider.MediaPlayer.IsPlaying()) vc.SourceProvider.MediaPlayer.Pause();
            float cur_time = vc.SourceProvider.MediaPlayer.Position;
            cur_time -= 0.001f;
            if (cur_time <= 0) { return; }
            vc.SourceProvider.MediaPlayer.Position = cur_time;
        }
        /// 
        /// 下一帧
        /// 
        /// 
        /// 
        private void btn_frame_nxt(object sender, RoutedEventArgs e)
        {
            if (vc.SourceProvider.MediaPlayer == null) return;
            if (vc.SourceProvider.MediaPlayer.IsPlaying()) vc.SourceProvider.MediaPlayer.Pause();
            float cur_time = vc.SourceProvider.MediaPlayer.Position;
            cur_time += 0.001f;
            if (cur_time >= 1) { return; }
            vc.SourceProvider.MediaPlayer.Position = cur_time;
        }
        /// 
        /// 保存截图
        /// 
        /// 
        /// 
        private void btn_snapshot(object sender, RoutedEventArgs e)
        {
            if (vc.SourceProvider.MediaPlayer == null) return;
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.Description = "选择截图保存位置";
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileInfo fi = new FileInfo(fbd.SelectedPath + "\\123.jpg");
                vc.SourceProvider.MediaPlayer.TakeSnapshot(fi);
            }
        }
        /// 
        /// 保存视频
        /// 
        /// 
        /// 
        private void btn_save(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.Description = "选择视频保存位置";
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                WebClient web = new WebClient();
                web.DownloadFile(url_video, fbd.SelectedPath + "\\test.mp4");
            }
        }

        private TimeSpan ts;
        private DispatcherTimer tt = new DispatcherTimer();
        /// 
        /// 调整进度
        /// 
        public void Init_Timer()
        {
            if (!tt.IsEnabled)
            {
                tt.Interval = TimeSpan.FromMilliseconds(1000);
                tt.Tick += new EventHandler((sender, e) =>
                {
                    progressbar.Value = Convert.ToInt32(vc.SourceProvider.MediaPlayer.Position * 100000);
                });
                tt.IsEnabled = true;
            }
        }
        /// 
        /// 进度条调整
        /// 
        /// 
        /// 
        private void progressbar_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (vc.SourceProvider.MediaPlayer == null) return;
            vc.SourceProvider.MediaPlayer.Position = float.Parse(progressbar.Value.ToString()) / 100000;
        }
        /// 
        /// 播放速率
        /// 
        /// 
        /// 
        private void combobox_rate_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (vc.SourceProvider.MediaPlayer == null) return;
            KeyValuePair kv = (KeyValuePair)combobox_rate.SelectedItem;
            vc.SourceProvider.MediaPlayer.Rate = kv.Value;
        }
        #endregion

 

 

你可能感兴趣的:(C#,VLC,WPF)