喵喵喵播放器
喵喵喵播放器
this is my project~~~~ https://github.com/yuhao2480444683/-
emmmm,对于像我这样的萌新来说,为了做一个从零开始的播放器,我决定求助于度娘...bing....
so,我找到了这个网站~(其实是上次作业用到的网站) https://developer.microsoft.com/en-us/windows/apps/develop
and then,我点开了这个~ https://msdn.microsoft.com/en-us/library/windows/apps/mt187272.aspx
网页中提到MediaElement可以这样定义:(不可自动播放)
<MediaElement x:Name="mediaPlayer"
Source="Videos/video1.mp4"
Width="400"
AutoPlay="False"
AreTransportControlsEnabled="True"/>
于是我写了如下代码:
<MediaElement x:Name="mediaPlayer"
MaxWidth="800"
MaxHeight="450"
AutoPlay="False"
AreTransportControlsEnabled="True" />
然后我想让我的播放器能够打开用户选择的文件或者输入的文件地址,于是我看到可以这样输入文件地址并打开:
<TextBox x:Name="txtFilePath" Width="400" FontSize="20" KeyUp="TxtFilePath_KeyUp" Header="File path" PlaceholderText="Enter file path"/>
private void TxtFilePath_KeyUp(object sender, KeyRoutedEventArgs e) { if (e.Key == Windows.System.VirtualKey.Enter) { TextBox tbPath = sender as TextBox; if (tbPath != null) { LoadMediaFromString(tbPath.Text); } } } private void LoadMediaFromString(string path) { try { Uri pathUri = new Uri(path); mediaPlayer.Source = pathUri; } catch (Exception ex) { if (ex is FormatException) { // handle exception. // For example: Log error or notify user problem with file } } }
于是我在项目中添加了如下代码~~
<TextBox x:Name="txtFilePath"
Width="150"
FontSize="20"
KeyUp="TxtFilePath_KeyUp"></TextBox>
private void TxtFilePath_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
TextBox tbPath = sender as TextBox;
if (tbPath != null)
{
LoadMediaFromString(tbPath.Text);
}
}
}
private void LoadMediaFromString(string path)
{
try
{
Uri pathUri = new Uri(path);
mediaPlayer.Source = pathUri;
}
catch (Exception ex)
{
if (ex is FormatException)
{
}
}
}
然后学习了如何通过点击按钮打开文件(规定只能打开mp3和mp4格式的文件),于是代码如下:
<Button Content="Choose file"
Click="Button_Click"></Button>
private async void Button_Click(object sender, RoutedEventArgs e)
{
await SetLocalMedia();
}
async private System.Threading.Tasks.Task SetLocalMedia()
{
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.FileTypeFilter.Add(".mp4");
openPicker.FileTypeFilter.Add(".mp3");
var file = await openPicker.PickSingleFileAsync();
if (file != null)
{
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
mediaPlayer.SetSource(stream, file.ContentType);
mediaPlayer.Play();
}
}
最后加入了一些我自己的排版~~~于是喵喵喵播放器诞生啦~surprise.....
thank you for watching~~
posted on 2018-04-01 23:57 yh2480444683 阅读(261) 评论(0) 编辑 收藏 举报