• VLC播放器


    最近在做WPF项目,需要做个本地的离线播放器,基本网上常用的有两种播放器,一种是 MediaElement方式的播放器,另一种就是Vlc的播放器,那么话不多说直接上代码!

    1.先将Vlc播放器下载到本地进行安装,(官网地址:https://www.videolan.org/vlc/index.zh.html),注意下载是64位的,还是32位的,由于我的工程项目是x86的,所以我下的32位的

    2.将安装后的路径(C:Program Files (x86)VideoLAN)下的VLC文件夹整体拷贝到项目的根目录下(inx86Debug)

    3.然后在项目的NuGet管理包中安装需要用到的三个包:Vlc.DotNet.Core,Vlc.DotNet.Core.Interops,Vlc.DotNet.Wpf

     4,介绍一下常用的API和Event

    //暂停 this.vlcControl.SourceProvider.MediaPlayer.Pause();

    //播放 this.vlcControl.SourceProvider.MediaPlayer.Play();

    //音量控制 this.vlcControl.SourceProvider.MediaPlayer.Audio.Volume = 100;

    //倍数控制 this.vlcControl.SourceProvider.MediaPlayer.Rate = 1.5f;

    //视频长度 单位:ms var len = this.vlcControl.SourceProvider.MediaPlayer.Length;

    //全屏 this.vlcControl.SourceProvider.MediaPlayer.Video.FullScreen = true;

    //进度变化事件 this.vlcControl.SourceProvider.MediaPlayer.PositionChanged += MediaPlayer_PositionChanged;

    5.上实例环节,鼓掌

    <Window x:Class="_2gd_read.Offline.showVideo"
            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:_2gd_read.video" xmlns:hc="https://handyorg.github.io/handycontrol"
            mc:Ignorable="d"  xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf" 
            Title="video" Height="450" Width="800" Loaded="Window_Loaded">
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="60" />
            </Grid.RowDefinitions>
            <wpf:VlcControl x:Name="myControl"/>
            <Grid Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="40" />
                </Grid.RowDefinitions>
                <Slider x:Name="media_slider" Minimum="0" Maximum="0" Value="0" IsSnapToTickEnabled="True" IsMoveToPointEnabled="True" ValueChanged="media_slider_ValueChanged"  VerticalAlignment="Center"/>
                <StackPanel  Orientation="Horizontal" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center">
                    <Button x:Name="ButtonForward" Click="ButtonForward_Click" Content="快进" Width="100" Height="28" Margin="5" />
                    <Button x:Name="ButtonStop" Click="ButtonStop_Click" Content="停止" Width="100" Height="28" Margin="5" />
                    <Button x:Name="ButtonBack" Click="ButtonBack_Click" Content="快退" Width="100" Height="28" Margin="5" />
                    <ContentControl  x:Name="ControlContainer"/>
                </StackPanel>
            </Grid>
        </Grid>
    </Window>
    WPF前台
    using Microsoft.Win32;
    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 System.Windows.Threading;
    using Vlc.DotNet.Core;
    using Vlc.DotNet.Wpf;
    
    namespace _2gd_read.Offline
    {
        /// <summary>
        /// showImage.xaml 的交互逻辑
        /// </summary>
        public partial class showVideo : Window
        {
            public readonly string url = "";
            public readonly string name = "";
       
            public showVideo(string surl)
            {
                InitializeComponent();
    
                string path = AppDomain.CurrentDomain.BaseDirectory + "sourse\book\bookdata\downloadresources";
    
                name = surl.Substring(surl.LastIndexOf('/') + 1);
    
                string wjname = surl.Substring(0, surl.LastIndexOf('/'));
    
                wjname = wjname.Substring(wjname.LastIndexOf('/') + 1);
    
                url = path + "\" + wjname + "\" + name;
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                this.VedioSource = new Uri(url);
            }
    
    
            private void ButtonStop_Click(object sender, RoutedEventArgs e)
            {
                if (ButtonStop.Content.ToString() == "停止")
                {
                    myControl.SourceProvider.MediaPlayer.Pause();
                    ButtonStop.Content = "播放";
                }
                else if (ButtonStop.Content.ToString() == "播放")
                {
                    myControl.SourceProvider.MediaPlayer.Play();
                    ButtonStop.Content = "停止";
    
                }
            }
    
            // 快进
            private void ButtonForward_Click(object sender, RoutedEventArgs e)
            {
                if (myControl.SourceProvider.MediaPlayer == null) return;
                if (myControl.SourceProvider.MediaPlayer.IsPlaying()) myControl.SourceProvider.MediaPlayer.Pause();
                float cur_time = myControl.SourceProvider.MediaPlayer.Position;
                cur_time += 0.020f;
                if (cur_time >= 1) { return; }
                myControl.SourceProvider.MediaPlayer.Position = cur_time;
                myControl.SourceProvider.MediaPlayer.Play();
            }
    
            // 快退
            private void ButtonBack_Click(object sender, RoutedEventArgs e)
            {
                if (myControl.SourceProvider.MediaPlayer == null) return;
                if (myControl.SourceProvider.MediaPlayer.IsPlaying()) myControl.SourceProvider.MediaPlayer.Pause();
                float cur_time = myControl.SourceProvider.MediaPlayer.Position;
                cur_time -= 0.020f;
                if (cur_time <= 0) { return; }
                myControl.SourceProvider.MediaPlayer.Position = cur_time;
                myControl.SourceProvider.MediaPlayer.Play();
            }
    
            //当前播放的路径
            public Uri VedioSource
            {
                get { return (Uri)GetValue(VedioSourceProperty); }
                set { SetValue(VedioSourceProperty, value); }
            }
    
            public static readonly DependencyProperty VedioSourceProperty =
               DependencyProperty.Register("VedioSource", typeof(Uri), typeof(showVideo), new PropertyMetadata(default(Uri), (d, e) =>
               {
                   showVideo control = d as showVideo;
    
                   if (control == null) return;
    
                   Uri config = e.NewValue as Uri;
    
                   control.InitVlc();
    
                   control.myControl.SourceProvider.MediaPlayer.Play(config);
    
                   //control.toggle_play.IsChecked = false;
    
               }));
    
            //设置当前显示时间
            public TimeSpan Time
            {
                get { return (TimeSpan)GetValue(TimeProperty); }
                set { SetValue(TimeProperty, value); }
            }
    
            public static readonly DependencyProperty TimeProperty =
                DependencyProperty.Register("Time", typeof(TimeSpan), typeof(showVideo), new PropertyMetadata(default(TimeSpan), async (d, e) =>
                {
                    showVideo control = d as showVideo;
    
                    if (control == null) return;
    
                    TimeSpan config = (TimeSpan)e.NewValue;
    
                    await Task.Run(() => control.myControl.SourceProvider.MediaPlayer.Time = (long)config.TotalMilliseconds);
    
                }));
    
    
          
    
            private async void media_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
            {
                long value = (long)this.media_slider.Value;
    
                await Task.Run(() => this.myControl.SourceProvider.MediaPlayer.Time = value);
            }
    
    
            private void PreviewSliderHorizontal_PreviewMouseUp(object sender, MouseButtonEventArgs e)
            {
                //  myControl.SourceProvider.MediaPlayer.Position = float.Parse(PreviewSliderHorizontal.Value.ToString());
            }
    
            private void slider_sound_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
            {
                this.RefreshAudio();
            }
    
            public void RefreshAudio()
            {
                if (this.myControl?.SourceProvider?.MediaPlayer?.Audio == null) return;
    
                //this.myControl.SourceProvider.MediaPlayer.Audio.Volume = (int)this.slider_sound.Value;
            }
    
            //全屏显示
            private void FButton_Click_Full(object sender, RoutedEventArgs e)
            {
                this.myControl.SourceProvider.MediaPlayer.Video.FullScreen = true;
            }
    
    
            //当前进度事件
            private void MediaPlayer_PositionChanged(object sender, VlcMediaPlayerPositionChangedEventArgs e)
            {
                this.RefreshSlider();
            }
    
            //刷新当前进度
            public void RefreshSlider()
            {
                this.Dispatcher.Invoke(() =>
                {
                    this.media_slider.ValueChanged -= this.media_slider_ValueChanged;
    
                    this.media_slider.Value = this.myControl.SourceProvider.MediaPlayer == null ? 0 : this.myControl.SourceProvider.MediaPlayer.Time;
    
                    this.media_slider.ValueChanged += this.media_slider_ValueChanged;
                });
            }
    
            //进度条长度事件
            private void MediaPlayer_LengthChanged(object sender, VlcMediaPlayerLengthChangedEventArgs e)
            {
                this.InitSlider();
            }
    
            //初始化播放进度条
            public void InitSlider()
            {
                if (this.myControl?.SourceProvider?.MediaPlayer == null) return;
    
                this.Dispatcher.Invoke(() =>
                {
                    this.media_slider.Maximum = this.myControl.SourceProvider.MediaPlayer.Length;
                });
    
            }
    
            //初始化
            public void InitVlc()
            {
                if (this.myControl?.SourceProvider?.MediaPlayer != null)
                {
                    this.myControl.SourceProvider.MediaPlayer.PositionChanged -= MediaPlayer_PositionChanged;
    
                    this.myControl.SourceProvider.MediaPlayer.LengthChanged -= MediaPlayer_LengthChanged;
                }
    
    
                //this.myControl = new VlcControl();
    
                //this.ControlContainer.Content = this.myControl;
    
                var currentDirectory = AppDomain.CurrentDomain.BaseDirectory + "VLC";
    
                var libDirectory = new DirectoryInfo(currentDirectory);
    
                this.myControl.SourceProvider.CreatePlayer(libDirectory);
    
                //this.vlccontrol.SourceProvider.MediaPlayer.Video.IsMouseInputEnabled = false;
                //this.vlccontrol.SourceProvider.MediaPlayer.Video.IsKeyInputEnabled = false;
    
                this.myControl.SourceProvider.MediaPlayer.PositionChanged += MediaPlayer_PositionChanged;
    
                this.myControl.SourceProvider.MediaPlayer.LengthChanged += MediaPlayer_LengthChanged;
            }
        }
    }
    WPF后台

    最后效果:

  • 相关阅读:
    P5047 [Ynoi2019 模拟赛] Yuno loves sqrt technology II
    P4887 【模板】莫队二次离线(第十四分块(前体))
    SP20644 ZQUERY
    企业战略管理【0612】
    管理经济学【0812】
    商务沟通[0664]
    国际企业管理【0813】
    社区管理【0272】
    战略管理【1059】
    管理沟通【1279】
  • 原文地址:https://www.cnblogs.com/zhao987/p/15078550.html
Copyright © 2020-2023  润新知