WPF封装VLC播放器控件(方式一:VideoPlayer控制Winform窗体句柄)

Vlc是一款优秀的开源播放器,支持众多音频与视频解码器及文件格式。这里采用WPF引用开源的Vlc相关插件(重新编译)封装了一个简单的视频播放器控件,实现视频播放、暂停、停止、快进、慢进、视频条、声音条、最大化等功能。

实现效果:

1、相关插件及资源文件放在了网盘,地址:https://pan.baidu.com/s/1Fyidxn3XgAJQAGL0RInVtQ  提取码:5alm

2、自定义控件UCVlcPlayer窗体源码


    
        
        
        
        
    
    
        
            
            
        
        
            
        
        
            
            
                
                
                
            
            
                
                
                

3、自定义控件UCVlcPlayer交互逻辑

using Declarations;
using Declarations.Events;
using Declarations.Media;
using Declarations.Players;
using Implementation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace VlcPlayerWPFDemo
{
    /// 
    /// UCVlcPlayer.xaml 的交互逻辑
    /// 
    public partial class UCVlcPlayer : UserControl
    {
        private IMediaPlayerFactory m_factory;
        private IVideoPlayer m_player;
        private IMediaFromFile m_media;
        private volatile bool m_isDrag;
        private string filePath = "";
        private double acWidth = 600;
        private double acHeight = 440;
        private double fullWidth = 600;
        private double fullHeight = 440;

        public delegate void SetFullScreenEvent(bool isFull);
        public event SetFullScreenEvent SetFullScreen;

        public UCVlcPlayer()
        {
            InitializeComponent();
            m_factory = new MediaPlayerFactory(true);
            m_player = m_factory.CreatePlayer();

            this.DataContext = m_player;

            m_player.Events.PlayerPositionChanged += new EventHandler(Events_PlayerPositionChanged);
            m_player.Events.TimeChanged += new EventHandler(Events_TimeChanged);
            m_player.Events.MediaEnded += new EventHandler(Events_MediaEnded);
            m_player.Events.PlayerStopped += new EventHandler(Events_PlayerStopped);

            m_player.WindowHandle = this.panelFormControl.Handle;
            volumeSlider.Value = m_player.Volume;
        }
        /// 
        /// 打开视频
        /// 
        public void OpenPlayer(string path)
        {
            filePath = path;
            if (!string.IsNullOrEmpty(filePath))
            {
                m_media = m_factory.CreateMedia(filePath);
                m_media.Events.DurationChanged += new EventHandler(Events_DurationChanged);
                m_player.Open(m_media);
                m_media.Parse(true);
            }
        }
        /// 
        /// 释放资源
        /// 
        public void DisposePlayer()
        {
            try
            {
                if (m_media != null)
                {
                    m_media.Dispose();
                    m_media = null;
                }
                if (m_player != null)
                {
                    m_player.Stop();
                    m_player.Dispose();
                    m_player = null;
                }
                if (m_factory != null)
                {
                    m_factory.Dispose();
                    m_factory = null;
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// 
        /// 设置原始宽高
        /// 
        public void InitSize(double width, double height)
        {
            acWidth = width;
            acHeight = height;
        }
        /// 
        /// 设置最大化以后宽高
        /// 
        public void InitFullSize(double width, double height)
        {
            fullWidth = width;
            fullHeight = height;
        }

        #region 绑定事件
        void Events_PlayerStopped(object sender, EventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(delegate
            {
                InitControls();
                if (this.imgPlay.Tag.ToString() != "play")
                {
                    this.imgPlay.Tag = "play";
                    this.imgPlay.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/Play.png"));
                }
            }));
        }
        void Events_MediaEnded(object sender, EventArgs e)
        {
            m_player.Stop();//为了可以再次播放在播放结束后调用一次stop
        }
        private void InitControls()
        {
            playProgressSlider.Value = 0;
            lblCurTime.Content = "00:00:00";
            if (m_player != null) m_player.PlaybackRate = 1;
        }
        void Events_TimeChanged(object sender, MediaPlayerTimeChanged e)
        {
            this.Dispatcher.BeginInvoke(new Action(delegate
            {
                lblCurTime.Content = TimeSpan.FromMilliseconds(e.NewTime).ToString().Substring(0, 8);
            }));
        }
        void Events_PlayerPositionChanged(object sender, MediaPlayerPositionChanged e)
        {
            this.Dispatcher.BeginInvoke(new Action(delegate
            {
                if (!m_isDrag)
                {
                    playProgressSlider.Value = (double)e.NewPosition;
                }
            }));
        }
        void Events_DurationChanged(object sender, MediaDurationChange e)
        {
            this.Dispatcher.BeginInvoke(new Action(delegate
            {
                lblTotalTime.Content = TimeSpan.FromMilliseconds(e.NewDuration).ToString().Substring(0, 8);
            }));
        }
        #endregion

        #region 事件
        private void volumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            if (m_player != null)
            {
                m_player.Volume = (int)e.NewValue;
            }
        }
        private void playProgressSlider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            m_player.Position = (float)playProgressSlider.Value;
            m_isDrag = false;
        }
        private void playProgressSlider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {
            m_isDrag = true;
        }
        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            m_player.PlaybackRate -= 0.1f;
        }
        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            m_player.Stop();
        }
        private void btnForward_Click(object sender, RoutedEventArgs e)
        {
            m_player.PlaybackRate += 0.1f;
        }
        private void imgPlay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.imgPlay.Tag.ToString() == "play")
            {
                m_player.Play();
                this.imgPlay.Tag = "pause";
                this.imgPlay.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/Pause.png"));
            }
            else
            {
                m_player.Pause();
                this.imgPlay.Tag = "play";
                this.imgPlay.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/Play.png"));
            }
        }
        private void imgFullScreen_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.imgFullScreen.Tag.ToString() == "full")
            {
                this.imgFullScreen.Tag = "exit";
                this.imgFullScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/fullscreen_exit.png"));
                this.Width = fullWidth;
                this.Height = fullHeight;
                SetFullScreen?.Invoke(true);
            }
            else
            {
                this.imgFullScreen.Tag = "full";
                this.imgFullScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Resource/fullscreen.png"));
                this.Width = acWidth;
                this.Height = acHeight;
                SetFullScreen?.Invoke(false);
            }
        }
        #endregion
    }
}

4、主窗体加载


    
        
    

5、主窗体调用逻辑

using System.Windows;
using System.ComponentModel;

namespace VlcPlayerWPFDemo
{
    public partial class VlcPlayerDemo : Window
    {
        public VlcPlayerDemo()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.player.InitFullSize(this.gridsize.ActualWidth, this.gridsize.ActualHeight);         
            this.player.InitSize(this.player.ActualWidth, this.player.ActualHeight);
            this.player.OpenPlayer(@"D:\哪吒预告.mp4");
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            this.player.DisposePlayer();
        }
    }
}

相关博文:WPF封装VLC播放器控件(方式二:VlcVideoSourceProvider绑定Image控件)

你可能感兴趣的:(音频视频,WPF应用实例,WPF,Vlc,视频播放)