admin管理员组文章数量:1652895
使用wpf制作一个视频播放器
一、 LibVLCSharp介绍
LibVLCSharp是基于VideoLAN的LibVLC库的.NET平台的跨平台音频和视频API。它提供了一个全面的多媒体API,可以跨移动、服务器和桌面使用,以呈现视频和输出音频。支持Mono、.NET Framework和.NET核心运行时。在vs工具中,只需要安装截图中得两个nuget包。
二、视频播放器的实现
视频播放器的实现需要用到两个比较nuget包中提供的类,MediaPlayer类。MediaPlayer类型用于控制播放、设置渲染器、提供事件等等。
简单看下其内部实现
提供了很多属性让我们去获取还有设置视频播放信息,见名知意,这里就一一说明每个的作用了。
其次该类中还定义了大量的时间处理程序让我们可以自定义视频播放过程,自己所要进行个性化操作。如下图:
另外补充还有另外的vlc视频播放器的第三方库使用。
使用方法也是一模一样的。
三、代码演示
(这里采用的是上边说明第二个库)
界面代码:
<Window x:Class="VLCPlayer.MainWindow"
xmlns="http://schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats/markup-compatibility/2006"
xmlns:local="clr-namespace:VLCPlayer"
xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
mc:Ignorable="d"
Closed="Window_Closed"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9.5*"></RowDefinition>
<RowDefinition Height="0.5*"></RowDefinition>
</Grid.RowDefinitions>
<vlc:VlcControl Grid.Row="0" x:Name="vlcPlayer" />
<Button Grid.Row="1"
Width="120"
Click="Button_Click" ></Button>
</Grid>
</Window>
code-bebind后台代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vlc.DotNet.Core;
namespace VLCPlayer
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//VLC播放器的安装位置,我的VLC播放器安装在D:\Program Files (x86)\VideoLAN\VLC文件夹下。
string currentDirectory = @"D:\VideoLAN\VLC";
var vlcLibDirectory = new DirectoryInfo(currentDirectory);
var options = new string[]
{
//添加日志
"--file-logging", "-vvv", "--logfile=Logs.log"
// VLC options can be given here. Please refer to the VLC command line documentation.
};
//初始化播放器
this.vlcPlayer.SourceProvider.CreatePlayer(vlcLibDirectory, options);
//
//加载libvlc库并初始化内容。在调用此方法之前,要设置好选项和lib目录
//设置播放源
this.vlcPlayer.SourceProvider.MediaPlayer.Play(new Uri(@"F:\迅雷下载\阳光电影www.ygdy8.敢梦有爱.BD.1080p.中英双字幕.mkv"));//本地文件。
#region 播放网络流
//this.vlcPlayer.SourceProvider.MediaPlayer.Play(new Uri("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov"));//Rtsp流文件。
// this.vlcPlayer.SourceProvider.MediaPlayer.Play(new Uri("rtmp://10.160.64.244:1935/live/room"));
#endregion
//暂停
this.vlcPlayer.SourceProvider.MediaPlayer.Pause();
//音量控制
this.vlcPlayer.SourceProvider.MediaPlayer.Audio.IsMute = false;
//播放速度
this.vlcPlayer.SourceProvider.MediaPlayer.Rate = float.Parse("2");
//
this.vlcPlayer.SourceProvider.MediaPlayer.Playing += new EventHandler<Vlc.DotNet.Core.VlcMediaPlayerPlayingEventArgs>(PlayingEvent);
this.vlcPlayer.SourceProvider.MediaPlayer.Paused += new EventHandler<Vlc.DotNet.Core.VlcMediaPlayerPausedEventArgs>(PausedEvent);
}
private void PlayingEvent(object sender, VlcMediaPlayerPlayingEventArgs e)
{
MessageBox.Show("播放视频!");
}
bool tag = true;
private void PausedEvent(object sender, Vlc.DotNet.Core.VlcMediaPlayerPausedEventArgs args)
{
MessageBox.Show("视频暂停了!");
}
private void Window_Closed(object sender, EventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//暂停
if (tag)
{
this.vlcPlayer.SourceProvider.MediaPlayer.Pause();
tag = !tag;
}
else
{
this.vlcPlayer.SourceProvider.MediaPlayer.Play();
tag = !tag;
}
}
}
}
运行效果:
有些小伙伴可能嫌弃播放器太丑了,这里大家先看看具体使用步骤就可以。
给大家看看美化后的效果。
如果在结合好看的界面库,就更容易做成好看的界面效果了。
版权声明:本文标题:使用VLC库实现视频播放器(WPF版) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729619614a1208108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论