首页 > 代码库 > WP8.1 和UWP 如何使用下载网页的上的音频 并保存
WP8.1 和UWP 如何使用下载网页的上的音频 并保存
WP8.1:
private async Task<StorageFile> GetVoiceData() { HttpClient httpclient = new HttpClient(); var GetData = http://www.mamicode.com/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 = 0; byte[] tempbyte = new byte[4096]; MemoryStream ms = new MemoryStream(4096); //当遇到0,表示读完了 do { currentPos = musicStream.Read(tempbyte, 0, 4096); ms.Write(tempbyte, 0, currentPos); } while (currentPos != 0); File.WriteAllBytes("c:\\test.mp3", ms.ToArray()); System.Console.WriteLine("OK"); }
WP8.1 和UWP 如何使用下载网页的上的音频 并保存
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。