WPF播放器


最近由於工作需要,需要做一個播放軟件,在網上參考了很多例子,園子里有很多代碼。其中最多的就是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, @"..\..\..\lib\x86\")); 27 else 28 e.VlcLibDirectory = new DirectoryInfo(Path.Combine(currentDirectory, @"..\..\..\lib\x64\")); 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.DotNet\Samples\Videos\BBB 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/

 


免責聲明!

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



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