UWP学习之路-naive media player2
第一次尝试一边写代码一边写博客呀,好像会变一种写作形式诶
技术问题一:改变一下UI~
解决过程:在Grid里新增一行,添加两个功能的Button:play online和cache and play,再添加一个TextBox接受URL地址~▼
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<RelativePanel Grid.Row="1"> <TextBlock FontSize="36" x:Name="OnlinePlay" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True" >OnlinePlay</TextBlock> <Button x:Name="OnlineButton" Width="200" Height="48" Margin="0,0,20,0" RelativePanel.LeftOf="CacheButton" RelativePanel.AlignVerticalCenterWithPanel="True" Click="OnlineButton_Click" Content="Play Online"/> <Button x:Name="CacheButton" Width="200" Height="48" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True" Click="CacheButton_Click" Content="Cache and Play"/> <TextBox x:Name="URLBox" Width="400" Height="48" RelativePanel.LeftOf="OnlineButton" Margin="0,0,20,0" RelativePanel.AlignVerticalCenterWithPanel="True"> </TextBox> </RelativePanel>
OK~
技术问题二:试试直接在线播放吧!
解决过程:搜到了这个网址,代码如下~▼
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
MediaElement media = new MediaElement();
Uri url = new Uri("http://go.microsoft.com/fwlink/p/?LinkID=272585");
media.Source = url;
media.Visibility = Windows.UI.Xaml.Visibility.Visible;
media.Play();
stackpanel.Children.Add(media);
改一改▼
Uri url = new Uri(uriString: URLBox.Text);
naiveMediaplayer.Source = url;
FileName.Text = naiveMediaplayer.Source.ToString();
File
Name.Text = FileName.Text.Substring(FileName.Text.LastIndexOf('/') + 1);
naiveMediaplayer.Play();
运行一下~
Good!
技术问题三:下载到用户音乐文件夹
解决过程:找到了这个网址,代码▼
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public async Task<StorageFile> Load() { try { var httpClient = new HttpClient(); var buffer = await httpClient.GetBufferAsync(new Uri("http://dx.sc.chinaz.com/Files/DownLoad/sound1/201207/1771.mp3")); if (buffer != null && buffer.Length > 0u) { var file = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync("1771.mp3", CreationCollisionOption.ReplaceExisting); using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { await stream.WriteAsync(buffer); await stream.FlushAsync(); } return file; } } catch { } return null; }
改一改▼
private async void CacheButton_Click(object sender, RoutedEventArgs e)
{
try
{
var httpClient = new HttpClient();
Uri url = new Uri(uriString: URLBox.Text);
var buffer = await httpClient.GetBufferAsync(url);
FileName.Text = url.ToString();
FileName.Text = FileName.Text.Substring(FileName.Text.LastIndexOf('/') + 1);
if (buffer != null && buffer.Length > 0u)
{
var file = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync(FileName.Text, CreationCollisionOption.ReplaceExisting);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await stream.WriteAsync(buffer);
await stream.FlushAsync();
naiveMediaplayer.SetSource(stream, file.ContentType);
}
return;
}
}
catch { }
return;
}
运行一下~ emmmm没下成...
从这个博客种了解到据说是没有权限的问题,改好了权限▼
还是没有下载到音乐文件夹里!
而且我勾选了背景媒体播放,也没有实现背景媒体播放,我陷入了迷茫...
难不成是我对设置没有保存?应该是我编译的时候会自动保存才对啊...
关掉重开! 是我太天真了,我保存了...但是我下载不了,我也不能后台播放...
仔细检查了一下我的代码,发现我光顾着改权限了,保存文件的位置忘改了,丢大脸了哇!
赶快改过来▼
(源代码)
var file = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync(FileName.Text, CreationCollisionOption.ReplaceExisting);
(改完的)
var file = await KnownFolders.MusicLibrary.CreateFileAsync(FileName.Text, CreationCollisionOption.ReplaceExisting);
也不知道我之前下那几十遍是下到哪个文件夹去了...希望我有一天能找到它们吧...
(不过我点了 - 键后还是不能后台播放...)
技术问题四:播放~
解决过程:把naive media player 1时代写的代码改一改~
var streamfile = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); naiveMediaplayer.SetSource(streamfile, file.ContentType); naiveMediaplayer.Play();
就可以播放啦!
美滋滋!