一、资源
官方Git地址:https://github.com/ZeBobo5/Vlc.DotNet
Nuget包地址-wpf:https://www.nuget.org/packages/Vlc.DotNet.Wpf/
下载解压有四个类库工程和两个实例项目
Vlc.DotNet.Core.Interops
Vlc.DotNet.Core
Vlc.DotNet.Forms
Vlc.DotNet.Wpf
和Vlc.DotNet.FormsSamples,Vlc.DotNet.WpfSamples
后者依赖前者。
今天只看如何能快速运行起来,其他的方法再慢慢添加。
从第一个开始 打开Vlc.DotNet.Core.Interops的工程文件.proj,打开后需要修改:1.解决方案-》属性-》配置,修改为Debug ,x86(如果你是64位操作系统,则选择x64).
2.项目-》属性-》生成,凡是看到x64的,都改为x86的(本人用的x86系统),否则会有平台不兼容的错误。 (默认情况下是AnyCpu,运行抛出异常)
修改完后生成项目,在obj中找到生成的dll文件。
然后第二个 打开Vlc.DotNet.Core的工程文件.proj ,添加第一个生成的dll引用。然后同第一个。。。生成dll.
然后第三个 打开Vlc.DotNet.Forms的工程文件.proj,添加前面两个生成的dll引用。然后同上。。。生成dll.如果是winform,就到此为止,
如果是wpf,则对Vlc.DotNet.Wpf同样操作,添加前三个dll,修改属性。。。生成dll.
以使用wpf为例,前面生成的四个dll,添加到,Vlc.DotNet.WpfSamples项目中,然后修改项目的属性,,生成运行。如果顺利的话就可以运行了。
不过前面生成的wpf.dll中的方法不多。只要运行起来,之后想添加什么,就去最底层dll找方法然后封装。这就不多讲了。
如果还有错误,试着检查项目属性的目标框架,总之要保持一致。
如果因为自己vlc陈旧而出现的Bug而来,那么给出建议:
如果想解决一些非功能上出现的问题,比如有些视频格式不能设置进度(本人碰到的),视频播放时画面花点等,这些不属于代码问题,所以不需要去重新编译vlc的源代码(当时做的时候,以为是底层的代码问题,最后想通了,肯定是不同格式解码编码的问题),这类问题,下载vlc官方播放器,在其安装目录中找到plugins文件夹,里面就是各种格式的编码解码文件,替换最新的文件到自己的项目中,就可以解决了。不过一些官方播放器也存在的问题,此方法就无效了,只能去研究源代码了,不过这就有难度了。
二、实例
Xaml代码:
1.引用命名空间 xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
2.创建控件实例 <wpf:VlcControl Grid.Row="0" x:Name="myControl"/>
<Window x:Class="Audio2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Audio2"
xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<wpf:VlcControl Grid.Row="0" x:Name="myControl"/>
<Button Grid.Row="1" Click="OnPlayButtonClick">Play</Button>
<Button Grid.Row="2" Click="OnForwardButtonClick" x:Name="Forward">Forward</Button>
<Button Grid.Row="3" Click="GetLength_Click" x:Name="GetLength">Get Length</Button>
<Button Grid.Row="4" Click="GetCurrentTime_Click" x:Name="GetCurrentTime">Get Current Time</Button>
<Button Grid.Row="5" Click="SetCurrentTime_Click" x:Name="SetCurrentTime">Set Current Time to 5s</Button>
</Grid>
</Window>
C# 后台代码:
1.初始化引用库,必须
2.可以播放在线视频,可以播放本地视频
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//初始化配置,指定引用库
myControl.MediaPlayer.VlcLibDirectoryNeeded += OnVlcControlNeedsLibDirectory;
myControl.MediaPlayer.EndInit();
}
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, @"..\..\..\lib\x64\"));
}
private void OnPlayButtonClick(object sender, RoutedEventArgs e)
{
//播放网络视频
myControl.MediaPlayer.Play(new Uri("http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi"));
//播放FM广播失败
//myControl.MediaPlayer.Play(new Uri("mms://radiolive.jnnc.com/news"));
////播放本地视频
//string filename = @"F:\MyDocument\视频\COOLUI理念篇.mp4";
//myControl.MediaPlayer.Play(new FileInfo(filename));
}
private void OnForwardButtonClick(object sender, RoutedEventArgs e)
{
myControl.MediaPlayer.Rate = 2;
}
private void GetLength_Click(object sender, RoutedEventArgs e)
{
GetLength.Content = myControl.MediaPlayer.Length + " ms";
}
private void GetCurrentTime_Click(object sender, RoutedEventArgs e)
{
GetCurrentTime.Content = myControl.MediaPlayer.Time + " ms";
}
private void SetCurrentTime_Click(object sender, RoutedEventArgs e)
{
myControl.MediaPlayer.Time = 5000;
SetCurrentTime.Content = myControl.MediaPlayer.Time + " ms";
}
}
更多相关文章: