Avalonia学习(十九)- LibMpv(视频播放)

2024-01-07 19:32:46

今天开始继续Avalonia练习。

本节:?LibMpv

1.引入

LibMpv.Avalonia;
LibMpv.MVVM;

2.项目引入

前台代码:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:LibMpvAvalonia.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="LibMpvAvalonia.Views.MainWindow"
        x:DataType="vm:MainWindowViewModel"
        xmlns:mpv="clr-namespace:LibMpv.Avalonia;assembly=LibMpv.Avalonia"
        Icon="/Assets/avalonia-logo.ico"
        Title="LibMpvAvalonia">

    <Design.DataContext>
        <!-- This only sets the DataContext for the previewer in an IDE,
             to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
        <vm:MainWindowViewModel/>
    </Design.DataContext>
  <Grid ColumnDefinitions="*" RowDefinitions="200,Auto">
    <StackPanel  Margin="5" Orientation="Horizontal" Spacing="5" Grid.Row="0">
      <Button Command="{Binding PlayPlayer}" Content="Load" CommandParameter="{Binding}"/>
      <Button Command="{Binding PausePlayer}" Content="Pause / Resume" CommandParameter="{Binding}"/>
      <Button Command="{Binding StopPlayer}" Content="Stop" CommandParameter="{Binding}"/>
    </StackPanel>
    <mpv:OpnGlVideoView Grid.Row="1" />
  </Grid>
</Window>

后台代码(Model)


using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.Input;
using LibMpv.Client;
using LibMpv.MVVM;
using System;

namespace LibMpvAvalonia.ViewModels
{

    public partial class MainWindowViewModel : BaseMpvContextViewModel
    {


        //public MpvContext Mpv { get; set; } = default!;

        public bool IsTextDurationsVisible => FunctionResolverFactory.GetPlatformId() != LibMpvPlatformID.Android;

        [RelayCommand]
        public void PlayPlayer()
        {
            if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
          desktop.MainWindow?.StorageProvider is not { } provider)
                throw new NullReferenceException("Missing StorageProvider instance.");

            var files =  provider.OpenFilePickerAsync(new FilePickerOpenOptions()
            {
                Title = "Open Text File",
                AllowMultiple = false
            }).Result;
           
            if (files.Count > 0)
            {

                this.LoadFile(files[0].Path.LocalPath);
                //this.LoadFile("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
                this.Play();

            }
           
        }

        [RelayCommand]
        public void PausePlayer()
        {
            this.Pause();
        }

        [RelayCommand]
        public void StopPlayer()
        {
            this.Stop();
        }

      
        public override void InvokeInUIThread(Action action)
        {
           Dispatcher.UIThread.Invoke(action);
        }


        // public VideoRenderer Renderer { get; set; }

        string _mediaUrl = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
        //public string MediaUrl
        //{
        //    get => _mediaUrl;
        //    set { this.(ref _mediaUrl, value); }
        //}

       
       
    }
}

同时演示了一下命令绑定

运行效果:

文章来源:https://blog.csdn.net/jinyuttt/article/details/135440865
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。