UWP学习--NaiveMediaPlayer

这里,我制作了一Naïve MediaPlayer,由于是Naïve ,所以只有简单的播放.mp3.mp4资源文件的功能,在之后的两次博客中,会逐渐改善升级。


第一:project homepage of my Github projecthttps://github.com/chunfengchuicao/NaiveMediaPlayer

 

第二:the problems

由于是简单的播放器,所以只有两个问题需要解决。一个是如何找到资源文件位置,另一个是如何播放资源文件。

    问题一:找到资源文件位置

    获取文件路径是必须,也是最重要的。在这里,通过网络搜索,在微软官方网站上我找到了fileopenpickerclass这个类,文件选取器可以显示,了解到了使用方法。

找到的网页:https://docs.microsoft.com/en-us/uwp/api/windows.storage.pickers.fileopenpicker

网页中例子的代码:

FileOpenPicker openPicker= new FileOpenPicker();

openPicker.ViewMode =PickerViewMode.Thumbnail;

openPicker.SuggestedStartLocation= PickerLocationId.PicturesLibrary;

openPicker.FileTypeFilter.Add(".jpg");

openPicker.FileTypeFilter.Add(".jpeg");

openPicker.FileTypeFilter.Add(".png");

 

StorageFile file = await openPicker.PickSingleFileAsync();

if (file != null)

{

    // Application now has read/write accessto the picked file

    OutputTextBlock.Text = "Pickedphoto: " + file.Name;

}

else

{

    OutputTextBlock.Text = "Operationcancelled.";

}

 

Note

You should always make sure that your app is not snapped (or thatit can be unsnapped) and set file picker properties regardless of whether theuser is picking a single file or multiple files.

 

我的代码:

varopenPicker = newWindows.Storage.Pickers.FileOpenPicker();

 

            openPicker.FileTypeFilter.Add(".mp4");

            openPicker.FileTypeFilter.Add(".mp3");

 

            var file = await openPicker.PickSingleFileAsync();

 

在这里,我学会了如何获取文件位置,并限制文件选取格式的选取。

 

    问题二:播放文件资源

    在课堂上和第一次blog中就了解到了MeidaEvent这个控件,在这里就可以使用这个控件来播放资源文件。

在微软的官方文档中有详细的使用方法。

找到的微软官方网站:https://docs.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Controls.MediaElement

网站中例子的代码:

private async void Button_Click(object sender, RoutedEventArgse)

{

    await SetLocalMedia();

}

 

async private System.Threading.Tasks.Task SetLocalMedia()

{

    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();

 

    openPicker.FileTypeFilter.Add(".wmv");

    openPicker.FileTypeFilter.Add(".mp4");

    openPicker.FileTypeFilter.Add(".wma");

    openPicker.FileTypeFilter.Add(".mp3");

 

    var file = await openPicker.PickSingleFileAsync();

 

    // mediaPlayer is a MediaElement definedin XAML

    if (file != null)

    {

        var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

        mediaPlayer.SetSource(stream,file.ContentType);

 

        mediaPlayer.Play();

    }

}

 

Important

In Windows 10, build 1607 and on we recommend that you use MediaPlayerElement in place of MediaElement. MediaPlayerElementhas the same functionality as MediaElement,while also enabling more advanced media playback scenarios. Additionally, allfuture improvements in media playback will happen in MediaPlayerElement.


但是我还是用的 MediaEvent 惊讶惊讶惊讶惊讶

我的代码:

if (file!= null)

            {

                var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                play.SetSource(stream,file.ContentType);

 

                play.Play();

            }

在这里,我学会了,现将文件路径转换成流,在设置媒体播放的文件路径,最后是播放媒体文件。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值