最近由于工作需要,需要做一个播放软件,在网上参考了很多例子,园子里有很多代码。其中最多的就是wpf自带的MediaElement控件,或者VLC视频播放器。
先附我自己查询资料的链接:
MediaEmelent控件例子
http://www.cnblogs.com/gnielee/archive/2010/05/06/wpf4-media-player-mediaelement.html
http://blog.zhigui.org/2011/04/wpf-simple-player/
VLC
http://www.cnblogs.com/Gavin001/archive/2013/05/01/3053465.html
由于我电脑可能是Ghost系统 或者由于被优化了。我发现我的MediaElement无法播放任何视频。通过控制面板关闭媒体中心之后,再也打不开了。真是坑!!!
只能放弃自带控件,查到有一个VLC的NET版。so。。。
附vlc.dotnet的github链接
https://github.com/ZeBobo5/Vlc.DotNet
首先,自己在nuget里面下载所需要的扩展:
完成之后 就看示例代码咯,github里面是有example的
1 <Window x:Class="Vlc.DotNet.Wpf.Samples.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:Vlc.DotNet.Wpf.Samples" 7 xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="350" Width="525"> 10 <Grid> 11 <Grid.RowDefinitions> 12 <RowDefinition Height="*"/> 13 <RowDefinition Height="Auto"/> 14 <RowDefinition Height="Auto"/> 15 <RowDefinition Height="Auto"/> 16 <RowDefinition Height="Auto"/> 17 <RowDefinition Height="Auto"/> 18 </Grid.RowDefinitions> 19 20 <wpf:VlcControl Grid.Row="0" x:Name="myControl"/> 21 22 <Button Grid.Row="1" Click="OnPlayButtonClick">Play</Button> 23 <Button Grid.Row="2" Click="OnForwardButtonClick" x:Name="Forward">Forward</Button> 24 <Button Grid.Row="3" Click="GetLength_Click" x:Name="GetLength">Get Length</Button> 25 <Button Grid.Row="4" Click="GetCurrentTime_Click" x:Name="GetCurrentTime">Get Current Time</Button> 26 <Button Grid.Row="5" Click="SetCurrentTime_Click" x:Name="SetCurrentTime">Set Current Time to 5s</Button> 27 </Grid> 28 </Window>
首先加上 xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
然后加上 <wpf:VlcControl Grid.Row="0" x:Name="myControl"/>
1 using System; 2 using System.IO; 3 using System.Reflection; 4 using System.Windows; 5 6 namespace Vlc.DotNet.Wpf.Samples 7 { 8 /// <summary> 9 /// Interaction logic for MainWindow.xaml 10 /// </summary> 11 public partial class MainWindow : Window 12 { 13 public MainWindow() 14 { 15 InitializeComponent(); 16 myControl.MediaPlayer.VlcLibDirectoryNeeded += OnVlcControlNeedsLibDirectory; 17 } 18 19 private void OnVlcControlNeedsLibDirectory(object sender, Forms.VlcLibDirectoryNeededEventArgs e) 20 { 21 var currentAssembly = Assembly.GetEntryAssembly(); 22 var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName; 23 if (currentDirectory == null) 24 return; 25 if (AssemblyName.GetAssemblyName(currentAssembly.Location).ProcessorArchitecture == ProcessorArchitecture.X86)
//vlc的dll路径,自己下载vlc播放器里面的libvlc.dll libvlccore.dll以及plugins文件夹 26 e.VlcLibDirectory = new DirectoryInfo(Path.Combine(currentDirectory, @"......libx86")); 27 else 28 e.VlcLibDirectory = new DirectoryInfo(Path.Combine(currentDirectory, @"......libx64")); 29 } 30 31 private void OnPlayButtonClick(object sender, RoutedEventArgs e) 32 { 33 myControl.MediaPlayer.Play(new Uri("http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi")); 34 //myControl.MediaPlayer.Play(new FileInfo(@"......Vlc.DotNetSamplesVideosBBB trailer.mov")); 35 } 36 37 private void OnForwardButtonClick(object sender, RoutedEventArgs e) 38 { 39 myControl.MediaPlayer.Rate = 2; 40 } 41 42 private void GetLength_Click(object sender, RoutedEventArgs e) 43 { 44 GetLength.Content = myControl.MediaPlayer.Length + " ms"; 45 } 46 47 private void GetCurrentTime_Click(object sender, RoutedEventArgs e) 48 { 49 GetCurrentTime.Content = myControl.MediaPlayer.Time + " ms"; 50 } 51 52 private void SetCurrentTime_Click(object sender, RoutedEventArgs e) 53 { 54 myControl.MediaPlayer.Time = 5000; 55 SetCurrentTime.Content = myControl.MediaPlayer.Time + " ms"; 56 } 57 } 58 }
代码地址为:https://github.com/ZeBobo5/Vlc.DotNet/blob/master/src/Samples/Vlc.DotNet.Wpf.Samples/