您的位置:首页 > 大数据 > 人工智能

UWP_Homework3------Naive Media Player

2018-04-01 16:07 323 查看
github链接:https://github.com/StaRicardo/MyMedia

要做这个naive media player,首先要解决的问题就是播放,这里可以参考开发人员手册里使用媒体播放

<MediaPlayerElement x:Name="_mediaPlayerElement"
AreTransportControlsEnabled="False"
HorizontalAlignment="Stretch"
Grid.Row="0"/>


mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video.mkv"));
mediaPlayer.Play();


通过调用SetMediaPlayer来设置该元素绑定的MediaPlayer实例

刚开始做的时候遇到一个很大的问题就是选择文件的button不知道放在哪里,原本打算放在播放器的下方状态栏里,,但是试了好久也没有找到改变mediaplayerelement状态栏样式的地方,后来看了几个同学提交的代码后才恍然大悟,,,选择文件的按钮没必要非放在播放器里面,放在外面就很简单了(虽然会丑一点

利用grid分成两个部分,上方放置一个button,下方放置MediaPlayerElement。

因为要将Button设成图标样式,所以使用AppBarButton

<AppBarButton x:Name="OpenFileButton"
Margin= "12" Icon="OpenFile"
Background="White"
Grid.Row="0"
Click="AppBarButton_Click"/>
<Grid Grid.Row="1">


接下来是选择文件的部分

根据开发人员手册选择文件和文件夹 里面给出的代码

var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");

Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
this.textBlock.Text = "Picked photo: " + file.Name;
}
else
{
this.textBlock.Text = "Operation cancelled.";
}


将PicturesLibrary改为VideosLibrary 并将.mp3,mp4加入进去,变为如下代码

var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
picker.FileTypeFilter.Add(".mp3");
picker.FileTypeFilter.Add(".mp4");

Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
this.textBlock.Text = "Picked photo: " + file.Name;
}
else
{
this.textBlock.Text = "Operation cancelled.";
}


然后修改if中的内容,因为上面给出的代码是

mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video.mkv"));


而我们返回的是StorageFile类型。在媒体转换 可以看到如何将StorageFile转换位MediaPlayer.source

最后在运行的时候会出现这样的错误!



如果按照vs自动提示的修补办法会产生在运行的时候会提示类型不匹配的原因(并不能看懂是为什么23333)在网上看到一个别人写的博客之后看到了他的做法。就是只把函数前面加async修饰而不按照自动修改就可以解决这一问题。

最终的代码如下:

private async void AppBarButton_Click(object sender, Routed
4000
EventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
picker.FileTypeFilter.Add(".mp3");
picker.FileTypeFilter.Add(".mp4");

Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
var mediaSource = MediaSource.CreateFromStorageFile(file);
MyMediaPlayer.Source = mediaSource;
MyMedia.SetMediaPlayer(MyMediaPlayer);
MyMediaPlayer.Play();

}
}


最后因为界面太丑,,再贴一张背景图片

最终效果如下:







Ps.为什么XAML里面不能用”//“进行注释啊?正常的语言不都是允许“//”进行注释的吗,”< !–”这种方式好麻烦啊
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UWP