WP8.1 和UWP 如何使用下载网页的上的音频 并保存

WP8.1 和UWP 如何使用下载网页的上的音频 并保存WP8.1 和UWP 如何使用下载网页的上的音频 并保存

WP8.1:

 private   async   Task<StorageFile>  GetVoiceData()
{ HttpClient httpclient = new HttpClient();
var GetData = await httpclient.GetStreamAsync(new Uri("http://dx.sc.chinaz.com/Files/DownLoad/sound1/201207/1771.mp3"));
var file = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync("1771.mp3", CreationCollisionOption.ReplaceExisting);
using (var stream = await file.OpenStreamForWriteAsync())
{
await GetData.CopyToAsync(stream);//将获取的HttpClient的Stream流复制到文件的stream流
await stream.FlushAsync();//异步刷新 } return This_Voice_files;
}
}

UWP:

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;
}

C#:

HttpClient client = new HttpClient();
using (var musicStream = client.GetStreamAsync("http://dx.sc.chinaz.com/Files/DownLoad/sound1/201207/1771.mp3").Result)
{
int currentPos = ;
byte[] tempbyte = new byte[]; MemoryStream ms = new MemoryStream();
//当遇到0,表示读完
do
{
currentPos = musicStream.Read(tempbyte, , );
ms.Write(tempbyte, , currentPos);
} while (currentPos != );
File.WriteAllBytes("c:\\test.mp3", ms.ToArray());
System.Console.WriteLine("OK");
}

欢迎分享,转载请注明来源:内存溢出

原文地址:http://outofmemory.cn/mo/589651.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-12
下一篇2022-04-12

随机推荐

发表评论

登录后才能评论

评论列表(0条)

    保存