1、創建wpf項目

2、使用Nuget安裝vlc,選擇vlc.docnet.wpf

3、xaml中添加vlc

4、編寫cs代碼

trsp協議的視頻源 公開的不好找,網上有的基本沒法使用,可以自己找個視頻用VLC軟件輸出一個rtsp協議的視頻流進行測試。
5、注意事項
首先 需要安裝VLC,然后將VLC整體拷貝到項目的輸出目錄,比如debug目錄下;


另外,下載安裝時,一定要注意VLC的版本,如果你的項目是默認anycpu,那么就需要安裝32位的VLC,
如果指定輸出64位,那么久安裝64位的VLC

最后源碼
MainWindow.xaml
1 <Window x:Class="wpfVlcDemo.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:wpfVlcDemo" 7 xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="450" Width="800"> 10 <Grid> 11 <vlc:VlcControl x:Name="vlcControl"></vlc:VlcControl> 12 </Grid> 13 </Window>
MainWindow.xaml.cs
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Windows; 8 using System.Windows.Controls; 9 using System.Windows.Data; 10 using System.Windows.Documents; 11 using System.Windows.Input; 12 using System.Windows.Media; 13 using System.Windows.Media.Imaging; 14 using System.Windows.Navigation; 15 using System.Windows.Shapes; 16 17 namespace wpfVlcDemo 18 { 19 /// <summary> 20 /// MainWindow.xaml 的交互邏輯 21 /// </summary> 22 public partial class MainWindow : Window 23 { 24 //獲取輸出目錄 25 private static string appPath = AppDomain.CurrentDomain.BaseDirectory; 26 //vlc文件的地址 27 private DirectoryInfo vlcLibDirectory = new DirectoryInfo(appPath + @"VLC"); 28 //配置項 29 string[] options = new string[] 30 { 31 // VLC options can be given here. Please refer to the VLC command line documentation. 32 }; 33 public MainWindow() 34 { 35 InitializeComponent(); 36 //創建播放器 37 this.vlcControl.SourceProvider.CreatePlayer(vlcLibDirectory, options); 38 //http協議視頻流 39 //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8")); 40 //rtmp協議 41 //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("rtmp://58.200.131.2:1935/livetv/hunantv")); 42 //本地視頻 43 this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("d:/1.avi")); 44 //添加播放結束事件 45 //其他事件 請自行查看 46 this.vlcControl.SourceProvider.MediaPlayer.EndReached += MediaPlayerEndEvent; 47 48 } 49 private void MediaPlayerEndEvent(object sender,Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e) 50 { 51 //播放結束,循環播放本地視頻 52 Task.Factory.StartNew(() => { 53 this.vlcControl.Dispose(); 54 Application.Current.Dispatcher.BeginInvoke(new Action(() => { 55 this.vlcControl.SourceProvider.CreatePlayer(vlcLibDirectory); 56 this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("d:/1.avi")); 57 this.vlcControl.SourceProvider.MediaPlayer.EndReached += MediaPlayerEndEvent; 58 })); 59 }); 60 } 61 } 62 }
