VLC播放器在WPF中的使用


一、在VLC官網下載最新的VLC播放器,然后安裝。安裝后在安裝文件目錄中把文件VideoLAN\VLC\拷貝到項目中。
\VLC文件夾中包括\plugins文件夾、axvlc.dll、libvlc.dll、libvlccore.dll、npvlc.dll,將整個VLC文件夾復制到\bin\Debug\下面;
注意:根據系統32/64選擇文件夾,否則會報錯 不是有效的 Win32 應用程序。

 

二、添加引用:Vlc.DotNet.Wpf.dll

 

三、前台 在XAML中添加命名空間的引用:

 xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"

 引用:

 <wpf:VlcControl x:Name="vlcControl" Grid.Row="0"></wpf:VlcControl>

后台:

if (this.vlcControl?.SourceProvider?.MediaPlayer != null)
                {
                    //this.vlcControl.SourceProvider.MediaPlayer.EndReached -= MediaPlayer_EndReached;//播放結束
                    //this.vlcControl.SourceProvider.MediaPlayer.PositionChanged -= MediaPlayer_PositionChanged;//播放位置改變事件-刷新播放進度
                    //this.vlcControl.SourceProvider.MediaPlayer.LengthChanged -= MediaPlayer_LengthChanged;//獲取播放總時長
                    //this.vlcControl.SourceProvider.MediaPlayer.TimeChanged -= MediaPlayer_TimeChanged;//獲取播放當前時間
                }
                //創建播放器
                string appPath = AppDomain.CurrentDomain.BaseDirectory; //獲取輸出目錄
                //根據系統32/64選擇文件夾,否則會報錯 不是有效的 Win32 應用程序。
                //IntPtr.Size == 4 表示當前程序是32位 x86的 
                DirectoryInfo vlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(appPath, "VLC", IntPtr.Size == 4 ? "win-x86" : "win-x64"));//vlc文件的地址
                //配置項
                string[] options = new string[]
                {
                    //添加日志
                    //   "--file-logging", "-vvv", "--logfile=Logs.log"
                    "--network-caching=300",//嘗試讀取或寫入受保護的內存。這通常指示其他內存已損壞。”
                };
                //創建播放器
                this.vlcControl.SourceProvider.CreatePlayer(vlcLibDirectory, options);
 //http協議視頻流
                this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8"));
                //添加播放事件
                this.vlcControl.SourceProvider.MediaPlayer.EndReached += MediaPlayer_EndReached;//播放結束
                //this.vlcControl.SourceProvider.MediaPlayer.PositionChanged += MediaPlayer_PositionChanged;//播放位置改變事件-刷新播放進度
                //this.vlcControl.SourceProvider.MediaPlayer.LengthChanged += MediaPlayer_LengthChanged;//獲取播放總時長
                //this.vlcControl.SourceProvider.MediaPlayer.TimeChanged += MediaPlayer_TimeChanged;//獲取播放當前時間

                this.vlcControl.SourceProvider.MediaPlayer.Rate = 1;

                VolumeSlider.Value = this.vlcControl.SourceProvider.MediaPlayer.Audio.Volume;
View Code

當前播放時間

 //獲取播放當前時間
        private void MediaPlayer_TimeChanged(object sender, Vlc.DotNet.Core.VlcMediaPlayerTimeChangedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(delegate
            {
                playTime.Text = TimeSpan.FromSeconds(e.NewTime).ToString().Substring(0, 8);
            }));
        }
View Code

停止: this.vlcControl.SourceProvider.MediaPlayer.Stop();

播放:this.vlcControl.SourceProvider.MediaPlayer.Play();

暫停:this.vlcControl.SourceProvider.MediaPlayer.Pause();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM