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

今天开始继续Avalonia练习。

本节: LibMpv

1.引入

LibMpv.Avalonia;
LibMpv.MVVM;

2.项目引入

前台代码:



    
        
        
    
  
    
      

后台代码(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); }
        //}

       
       
    }
}

同时演示了一下命令绑定

运行效果:

你可能感兴趣的:(学习,Avalonia)