WinUI 3入门过程的各种坑二

WinUI 3 MediaPlayerElement控件的一些操作

MediaPlayerElement控件UWP和WinUI 3均可使用,本文使用UWP演示

制作一个简易的MediaPlayer


添加MediaPlayerElement

MediaPlayerElement:

<MediaPlayerElement
x:Name="mediaPlayerElement"
Width="800" 
AutoPlay="False"
AreTransportControlsEnabled="True"
/>

效果:

全屏模式下:

添加源视频文件

使用CommandBar控件

xaml

<StackPanel>
            <CommandBar Background="Transparent" IsOpen="False" DefaultLabelPosition="Right">
                <CommandBar.SecondaryCommands>
                    <AppBarButton Name="Mybtn" Icon="Setting" Label="Settings" Click="Mybtn_Click">
                        <AppBarButton.KeyboardAccelerators>
                            <KeyboardAccelerator Modifiers="Control" Key="I" />
                        </AppBarButton.KeyboardAccelerators>
                    </AppBarButton>
                    <AppBarButton Icon="Home" Label="Home">
                        <AppBarButton.KeyboardAccelerators>
                            <KeyboardAccelerator Modifiers="Control" Key="I" />
                        </AppBarButton.KeyboardAccelerators>
                    </AppBarButton>
                </CommandBar.SecondaryCommands>
            </CommandBar>
        </StackPanel>

C#

WinUI 3版
private async void OpenFile_Click(object sender, RoutedEventArgs e)
        {
            await SetLocalMedia();
        }

        async private System.Threading.Tasks.Task SetLocalMedia()
        {
            var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
            WinRT.Interop.InitializeWithWindow.Initialize(openPicker, WinRT.Interop.WindowNative.GetWindowHandle(this));

            openPicker.FileTypeFilter.Add(".wmv");
            openPicker.FileTypeFilter.Add(".mp4");
            openPicker.FileTypeFilter.Add(".wma");
            openPicker.FileTypeFilter.Add(".mp3");

            var file = await openPicker.PickSingleFileAsync();

            // mediaPlayerElement is a MediaPlayerElement control defined in XAML
            if (file != null)
            {
                mediaPlayerElement.Source = MediaSource.CreateFromStorageFile(file);

                mediaPlayerElement.MediaPlayer.Play();
            }
        }
效果:

UWP版
private async void OpenFile_Click(object sender, RoutedEventArgs e)
        {
            await SetLocalMedia();
        }

        async private System.Threading.Tasks.Task SetLocalMedia()
        {
            var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
            openPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;

            openPicker.FileTypeFilter.Add(".wmv");
            openPicker.FileTypeFilter.Add(".mp4");
            openPicker.FileTypeFilter.Add(".wma");
            openPicker.FileTypeFilter.Add(".mp3");

            var file = await openPicker.PickSingleFileAsync();

            // mediaPlayerElement is a MediaPlayerElement control defined in XAML
            if (file != null)
            {
                mediaPlayerElement.Source = MediaSource.CreateFromStorageFile(file);

                mediaPlayerElement.MediaPlayer.Play();
            }
        }
效果:

效果:

添加事件处理程序

最终效果:

源文件下载

简易的mediaplayer-C#文档类资源-CSDN文库


本文参考:
使用选取器打开文件和文件夹 - UWP applications | Microsoft Learn
XAML 样式 - Windows apps | Microsoft Learn
创建自定义媒体传输控件 - Windows apps | Microsoft Learn

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值