Avalonia学习(十八)-LibVLC(视频播放)

发布时间:2024年01月07日

今天开始继续Avalonia练习。

本节:LibVLC

1.引入

Dove.Avalonia.Extensions.Media;
VideoLAN.LibVLC.Windows

2.项目引入

前台代码

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:PlayerAvalonia.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="PlayerAvalonia.Views.MainWindow"
        x:DataType="vm:MainWindowViewModel"
        Icon="/Assets/avalonia-logo.ico"
        Title="PlayerAvalonia">

    <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 RowDefinitions="30,*">
    <Button Content="测试" Click="Button_Click" Grid.Row="0"/>
    <PlayerView x:Name="playerView" Grid.Row="1"/>
   
  </Grid>
 

</Window>

后台代码

using Avalonia.Controls;
using Avalonia.Extensions.Media;
using Avalonia.Platform.Storage;
using LibVLCSharp.Shared;
using System.IO;

namespace PlayerAvalonia.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
           
        }

        private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
        {
            var topLevel = TopLevel.GetTopLevel(this);

            // 启动异步操作以打开对话框。
            var files =  topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
            {
                Title = "Open Text File",
                AllowMultiple = false
            }).Result;
            //var files = await storage.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
            //{
            //    Title = title,
            //    //您可以添加自定义文件类型,也可以从内置文件类型添加。请参阅“定义自定义文件类型”,了解如何创建自定义文件类型。
            //    FileTypeFilter = new[] { ImageAll, FilePickerFileTypes.TextPlain }
            //});
            if (files.Count > 0)
            {
                PlayerView VideoView = this.FindControl<PlayerView>("playerView");
              
                VideoView.Play(files[0].Path);
               
            }
        }
    }
}

运行效果

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