当前位置:   article > 正文

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

avalonia libmpv

今天开始继续Avalonia练习。

本节: LibMpv

1.引入

  1. LibMpv.Avalonia;
  2. LibMpv.MVVM;

2.项目引入

前台代码:

  1. <Window xmlns="https://github.com/avaloniaui"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:vm="using:LibMpvAvalonia.ViewModels"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
  7. x:Class="LibMpvAvalonia.Views.MainWindow"
  8. x:DataType="vm:MainWindowViewModel"
  9. xmlns:mpv="clr-namespace:LibMpv.Avalonia;assembly=LibMpv.Avalonia"
  10. Icon="/Assets/avalonia-logo.ico"
  11. Title="LibMpvAvalonia">
  12. <Design.DataContext>
  13. <!-- This only sets the DataContext for the previewer in an IDE,
  14. to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
  15. <vm:MainWindowViewModel/>
  16. </Design.DataContext>
  17. <Grid ColumnDefinitions="*" RowDefinitions="200,Auto">
  18. <StackPanel Margin="5" Orientation="Horizontal" Spacing="5" Grid.Row="0">
  19. <Button Command="{Binding PlayPlayer}" Content="Load" CommandParameter="{Binding}"/>
  20. <Button Command="{Binding PausePlayer}" Content="Pause / Resume" CommandParameter="{Binding}"/>
  21. <Button Command="{Binding StopPlayer}" Content="Stop" CommandParameter="{Binding}"/>
  22. </StackPanel>
  23. <mpv:OpnGlVideoView Grid.Row="1" />
  24. </Grid>
  25. </Window>

后台代码(Model)

  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Platform.Storage;
  5. using Avalonia.Threading;
  6. using CommunityToolkit.Mvvm.Input;
  7. using LibMpv.Client;
  8. using LibMpv.MVVM;
  9. using System;
  10. namespace LibMpvAvalonia.ViewModels
  11. {
  12. public partial class MainWindowViewModel : BaseMpvContextViewModel
  13. {
  14. //public MpvContext Mpv { get; set; } = default!;
  15. public bool IsTextDurationsVisible => FunctionResolverFactory.GetPlatformId() != LibMpvPlatformID.Android;
  16. [RelayCommand]
  17. public void PlayPlayer()
  18. {
  19. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  20. desktop.MainWindow?.StorageProvider is not { } provider)
  21. throw new NullReferenceException("Missing StorageProvider instance.");
  22. var files = provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  23. {
  24. Title = "Open Text File",
  25. AllowMultiple = false
  26. }).Result;
  27. if (files.Count > 0)
  28. {
  29. this.LoadFile(files[0].Path.LocalPath);
  30. //this.LoadFile("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
  31. this.Play();
  32. }
  33. }
  34. [RelayCommand]
  35. public void PausePlayer()
  36. {
  37. this.Pause();
  38. }
  39. [RelayCommand]
  40. public void StopPlayer()
  41. {
  42. this.Stop();
  43. }
  44. public override void InvokeInUIThread(Action action)
  45. {
  46. Dispatcher.UIThread.Invoke(action);
  47. }
  48. // public VideoRenderer Renderer { get; set; }
  49. string _mediaUrl = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
  50. //public string MediaUrl
  51. //{
  52. // get => _mediaUrl;
  53. // set { this.(ref _mediaUrl, value); }
  54. //}
  55. }
  56. }

同时演示了一下命令绑定

运行效果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/889306
推荐阅读
相关标签
  

闽ICP备14008679号