使用VLC库实现视频播放器(WPF版)

使用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.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: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.com.敢梦有爱.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;
            }
        }
    }
}

运行效果:

有些小伙伴可能嫌弃播放器太丑了,这里大家先看看具体使用步骤就可以。

给大家看看美化后的效果。

如果在结合好看的界面库,就更容易做成好看的界面效果了。

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值