一、在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;
当前播放时间

//获取播放当前时间 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); })); }
停止: this.vlcControl.SourceProvider.MediaPlayer.Stop();
播放:this.vlcControl.SourceProvider.MediaPlayer.Play();
暂停:this.vlcControl.SourceProvider.MediaPlayer.Pause();