0.前沿:
今天我们不节外生枝,只讲核心的需求的解决方案
1. 要求:
- 能播放在线音频
- 能下载在线音频到对应操作系统(其实也就windows)用户目录下的音乐文件夹
2. 效果:
项目地址: https://github.com/HallWoodZhang/SimpleMediaPlayer_UWP
3. 分析
- 播放在线音频:
给定url, 然后用微软自带的播放器去播放这个音源,很简单
VideoPlayer.Source = MediaSource.CreateFromUri(new Uri(this.url));
- 下载指定音频
首先,你需要在项目的功能界面里面选音乐库,不然win10不让你下载的
然后你需要利用HTTP协议下载序列化的文件流
https://docs.microsoft.com/en-us/windows/uwp/networking/httpclient
下载样例代码
httpResponse = await httpClient.GetAsync(requestUri);
httpResponse.EnsureSuccessStatusCode();
httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
保存样例代码
StorageFolder testFolder = await StorageFolder.GetFolderFromPathAsync(@"C:\test");
StorageFile sourceFile = await testFolder.GetFileAsync("TestImage.jpg");
StorageFile destinationFile = await KnownFolders.CameraRoll.CreateFileAsync("MyTestImage.jpg");
using (var sourceStream = await sourceFile.OpenReadAsync())
{
using (var sourceInputStream = sourceStream.GetInputStreamAt(0))
{
using (var destinationStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (var destinationOutputStream = destinationStream.GetOutputStreamAt(0))
{
await RandomAccessStream.CopyAndCloseAsync(sourceInputStream, destinationStream);
}
}
}
}
剩余的的部分,比如GUI的切换,页面的高斯模糊,并不涉及核心要求,这里就不再讨论了