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的
<Window x:Class="Vlc.DotNet.Wpf.Samples.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:Vlc.DotNet.Wpf.Samples" 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>
首先加上 xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
然后加上 <wpf:VlcControl Grid.Row="0" x:Name="myControl"/>
using System; using System.IO; using System.Reflection; using System.Windows; namespace Vlc.DotNet.Wpf.Samples { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); myControl.MediaPlayer.VlcLibDirectoryNeeded += OnVlcControlNeedsLibDirectory; } private void OnVlcControlNeedsLibDirectory(object sender, 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) //vlc的dll路径,自己下载vlc播放器里面的libvlc.dll libvlccore.dll以及plugins文件夹 e.VlcLibDirectory = new DirectoryInfo(Path.Combine(currentDirectory, @"..\..\..\lib\x86\")); else e.VlcLibDirectory = new DirectoryInfo(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")); //myControl.MediaPlayer.Play(new FileInfo(@"..\..\..\Vlc.DotNet\Samples\Videos\BBB trailer.mov")); } 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"; } } }
代码地址为:https://github.com/ZeBobo5/Vlc.DotNet/blob/master/src/Samples/Vlc.DotNet.Wpf.Samples/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧