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

Posted ARM830

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 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 如何使用下载网页的上的音频 并保存的主要内容,如果未能解决你的问题,请参考以下文章

SystemMediaTransportControl GetForCurrentView() 在桌面设备系列上的音频后台任务中失败(UWP C#

如何使用 Windows Phone 的麦克风在网页中录制音频?

WP8.1 如何在 C# 中取消后台音频任务

UWP&WP8.1 重新绘制图片 WriteableBitmap用法 图片转byte[]数组,byte[]数组转图片

如何禁用网页中的视频/音频下载?

uwp之拍照(使用后置摄像头)