wpf vlc 播放本地mp4视频 LibVLCSharp.WPF

1、nuget安装库

 

2、视频设置

 

3、xaml

<UserControl x:Class="WpfApp4TestVLC.View.HomeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WpfApp4TestVLC.View"
             xmlns:localvm="clr-namespace:WpfApp4TestVLC.ViewModel"
             mc:Ignorable="d"
             xmlns:uc="clr-namespace:LibVLCSharp.WPF;assembly=LibVLCSharp.WPF"
                   xmlns:prism="http://prismlibrary.com/"
                 xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.DataContext>
        <localvm:HomeViewModel></localvm:HomeViewModel>
    </UserControl.DataContext>
    <i:Interaction.Triggers>

        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoadedCommand}" />
        </i:EventTrigger>
        <i:EventTrigger EventName="Unloaded">
            <i:InvokeCommandAction Command="{Binding UnloadedCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <uc:VideoView x:Name="videoViewControl" />
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition ></ColumnDefinition>
                <ColumnDefinition Width="auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Slider Grid.Row="1"  Value="{Binding CurrentValue}" Minimum="0" Maximum="{Binding MaxValue}"></Slider>
            <StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                <TextBlock Text="{Binding   CurrentValue}"></TextBlock>
                <TextBlock Text="/"></TextBlock>
                <TextBlock Text="{Binding   MaxValue}"></TextBlock>
            </StackPanel>
        </Grid>

        <StackPanel Grid.Row="2" VerticalAlignment="Bottom" HorizontalAlignment="Center" Orientation="Horizontal">
            <Button x:Name="PlayButton" Content="播放❉" Margin="2" Command="{Binding PlayButtonCommand}"></Button>
            <Button x:Name="PauseButton" Content="暂停▥" Margin="2" Command="{Binding PauseButtonCommand}"></Button>
            <Button x:Name="JumpPreviewButton" Content="后退◀" Margin="2" Command="{Binding JumpPreviewButtonCommand}"></Button>
            <Button x:Name="ContinueButton" Content="继续▷" Margin="2" Command="{Binding ContinueButtonComand}"></Button>
            <Button x:Name="JumpNextButton" Content="快进▶" Margin="2" Command="{Binding JumpNextButtonCommand}"></Button>
            <Button x:Name="StopButton" Content="停止▧" Margin="2" Command="{Binding StopButtonCommand}"></Button>
        </StackPanel>
    </Grid>
</UserControl>

4、model cs

using LibVLCSharp.Shared;
using LibVLCSharp.WPF;
using Prism.Commands;
using Prism.Mvvm;
using System.Windows;

namespace WpfApp4TestVLC.ViewModel
{
    public class HomeViewModel : BindableBase
    {
        private LibVLC _libVLC;
        private MediaPlayer _mediaPlayer;

        private System.Timers.Timer Timer;

        public VideoView videoViewControl;
        private double _CurrentValue;

        public double CurrentValue
        {
            get { return _CurrentValue; }
            set { SetProperty(ref _CurrentValue, value); }
        }

        private double _MaxValue;

        public double MaxValue
        {
            get { return _MaxValue; }
            set { SetProperty(ref _MaxValue, value); }
        }

        private DelegateCommand _LoadedCommand;

        public DelegateCommand LoadedCommand =>
            _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));

        private void ExecuteLoadedCommand()
        {
            _libVLC = new LibVLC(enableDebugLogs: true);
            _mediaPlayer = new MediaPlayer(_libVLC);

            videoViewControl.MediaPlayer = _mediaPlayer;
            //播放速度
            videoViewControl.MediaPlayer.SetRate(1);

            Timer = new System.Timers.Timer(500);
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();
        }

        private DelegateCommand _JumpNextButtonCommand;

        public DelegateCommand JumpNextButtonCommand =>
            _JumpNextButtonCommand ?? (_JumpNextButtonCommand = new DelegateCommand(ExecuteJumpNextButtonCommand));

        private void ExecuteJumpNextButtonCommand()
        {
            if (videoViewControl.MediaPlayer.IsPlaying)
            {
                var dur = videoViewControl.MediaPlayer.Media.Duration;

                var current_time = videoViewControl.MediaPlayer.Time;
                long nexttime = current_time + 5000;
                if (nexttime <= dur)
                {
                    videoViewControl.MediaPlayer.SeekTo(TimeSpan.FromMilliseconds(current_time + 5000));
                }
            }
        }

        private DelegateCommand _JumpPreviewButtonCommand;

        public DelegateCommand JumpPreviewButtonCommand =>
            _JumpPreviewButtonCommand ?? (_JumpPreviewButtonCommand = new DelegateCommand(ExecuteJumpPreviewButtonCommand));

        private void ExecuteJumpPreviewButtonCommand()
        {
            if (videoViewControl.MediaPlayer.IsPlaying)
            {
                var current_time = videoViewControl.MediaPlayer.Time;
                if (current_time - 5000 > 0)
                {
                    videoViewControl.MediaPlayer.SeekTo(TimeSpan.FromMilliseconds(current_time - 5000));
                }
                else
                {
                    videoViewControl.MediaPlayer.SeekTo(TimeSpan.FromMilliseconds(0));
                }
            }
        }

        private DelegateCommand _ContinueButtonComand;

        public DelegateCommand ContinueButtonComand =>
            _ContinueButtonComand ?? (_ContinueButtonComand = new DelegateCommand(ExecuteContinueButtonComand));

        private void ExecuteContinueButtonComand()
        {
            videoViewControl.MediaPlayer.Play();
        }

        private DelegateCommand _PlayButtonCommand;

        public DelegateCommand PlayButtonCommand =>
            _PlayButtonCommand ?? (_PlayButtonCommand = new DelegateCommand(ExecuteCommandName));

        private void ExecuteCommandName()
        {
            if (!videoViewControl.MediaPlayer.IsPlaying)
            {
                string fileLocation = AppDomain.CurrentDomain.BaseDirectory + "TestVideo\\BigBuckBunny.mp4";
                using (var media = new Media(_libVLC, fileLocation))
                {
                    videoViewControl.MediaPlayer.Play(media);
                }
            }
        }

        private DelegateCommand _StopButtonCommand;

        public DelegateCommand StopButtonCommand =>
            _StopButtonCommand ?? (_StopButtonCommand = new DelegateCommand(ExecuteStopButtonCommand));

        private void ExecuteStopButtonCommand()
        {
            videoViewControl.MediaPlayer.Stop();
        }

        private DelegateCommand _PauseButtonCommand;

        public DelegateCommand PauseButtonCommand =>
            _PauseButtonCommand ?? (_PauseButtonCommand = new DelegateCommand(ExecutePauseButton));

        private void ExecutePauseButton()
        {
            if (videoViewControl.MediaPlayer.IsPlaying)
            {
                videoViewControl.MediaPlayer.Pause();
            }
        }

        private DelegateCommand _UnloadedCommand;

        public DelegateCommand UnloadedCommand =>
            _UnloadedCommand ?? (_UnloadedCommand = new DelegateCommand(ExecuteUnLoadCommand));

        private void ExecuteUnLoadCommand()
        {
            Timer.Stop();
            _mediaPlayer.Stop();
            _mediaPlayer.Dispose();
            _libVLC.Dispose();
        }

        private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            if (Application.Current != null)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    if (videoViewControl.MediaPlayer.IsPlaying)
                    {
                        this.MaxValue = videoViewControl.MediaPlayer.Media.Duration;
                        long current = videoViewControl.MediaPlayer.Time;
                        this.CurrentValue = current;
                    }
                }));
            }
        }
    }
}

5、

<Window x:Class="WpfApp4TestVLC.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:WpfApp4TestVLC"
        xmlns:localview="clr-namespace:WpfApp4TestVLC.View"
        xmlns:localvm="clr-namespace:WpfApp4TestVLC.ViewModel"
        mc:Ignorable="d"
        Title="播放器demo" Height="550" Width="900">
 
    <Grid>
        <localview:HomeView></localview:HomeView>
    </Grid>
</Window>

 

6、点击播放按钮 播放效果

 

posted @ 2024-05-23 10:55  JohnnyLei  阅读(536)  评论(0)    收藏  举报