这里,我制作了一个Naïve MediaPlayer,由于是Naïve ,所以只有简单的播放.mp3,.mp4资源文件的功能,在之后的两次博客中,会逐渐改善升级。
第一:project homepage of my Github project:https://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.
![惊讶](https://static-blog.csdn.net/xheditor/xheditor_emot/default/ohmy.gif)
![惊讶](https://static-blog.csdn.net/xheditor/xheditor_emot/default/ohmy.gif)
![惊讶](https://static-blog.csdn.net/xheditor/xheditor_emot/default/ohmy.gif)
![惊讶](https://static-blog.csdn.net/xheditor/xheditor_emot/default/ohmy.gif)
我的代码:
if (file!= null)
{
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
play.SetSource(stream,file.ContentType);
play.Play();
}
在这里,我学会了,现将文件路径转换成流,在设置媒体播放的文件路径,最后是播放媒体文件。