1.NuGet上下載Vlc.DotNet.Wpf, 在https://github.com/ZeBobo5/Vlc.DotNet 上下載的源碼都是最新版本的,裏面有調用的示例,每一個版本調用方法都不同。 下面代碼以2.2.1爲例。git
安裝完成後,程序中會自動引用相關dllgithub
2. 播放視頻相關代碼ide
<Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="40"/> </Grid.RowDefinitions> <wpf:VlcControl Grid.Row="0" x:Name="myControl" /> <Button x:Name="btnPlay" Width="120" Height="30" Grid.Row="1" Content="play" /> </Grid>
/// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); btnPlay.Click += BtnPlay_Click; //初始化配置,指定引用庫 myControl.MediaPlayer.VlcLibDirectoryNeeded += OnVlcControlNeedsLibDirectory; myControl.MediaPlayer.EndInit(); } private void BtnPlay_Click(object sender, RoutedEventArgs e) { //myControl.MediaPlayer.Play(new Uri(AppConfig.rtspUrl)); myControl.MediaPlayer.Play(new Uri("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov")); //throw new NotImplementedException(); } private void OnVlcControlNeedsLibDirectory(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e) { var currentAssembly = Assembly.GetEntryAssembly(); var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName; if (currentDirectory == null) return; if (AssemblyName.GetAssemblyName(currentAssembly.Location).ProcessorArchitecture == ProcessorArchitecture.X86) e.VlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory, @"..\..\..\lib\x86\")); else e.VlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory, "libvlc","x64")); } }
3.替換解碼相關dll,代碼中是在debug目錄下新建libvlc目錄,再建x64和x86兩個子目錄,分別存放相關dll,能夠在vlc官網上下載最新exe安裝後查找下圖中的dll進行存放,這裏注意官網上下載的默認的32程序,替換不對的話會出現下面的問題1.測試
能夠找到歷史的版本有針對性的下載,以下圖。http://download.videolan.org/pub/videolan/vlc/url
點擊按鈕播放後碰到的幾個問題。spa
1.程序出錯,不是有效的win32應用程序,緣由:把32位的程序dll放到了第三步x64目錄下面.net
2.輸出窗口顯示 線程已退出,視頻沒法播放。 這代表解碼相關dll沒有,注意下第三步從新下載替換。線程
3.沒法找到指定文件,問題是第三步裏面的文件有缺失的。debug
最後再補幾個能夠測試的地址3d
http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi
rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov
rtmp://live.hkstv.hk.lxdns.com/live/hks
附帶2個demo鏈接