前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF播放器

WPF播放器

作者头像
叁金
发布2018-09-04 14:49:17
1.9K0
发布2018-09-04 14:49:17
举报
文章被收录于专栏:叁金大数据

最近由于工作需要,需要做一个播放软件,在网上参考了很多例子,园子里有很多代码。其中最多的就是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的

代码语言:javascript
复制
 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"/>

代码语言:javascript
复制
 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/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-08-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档