WP8.1:
Private AsyncTask<storagefile>Getvoicedata () {HttpClient HttpClient=NewHttpClient (); varGetData =awaitHttpClient. Getstreamasync (NewUri ("Http://dx.sc.chinaz.com/Files/DownLoad/sound1/201207/1771.mp3")); varFile =awaitApplicationData.Current.LocalCacheFolder.CreateFileAsync ("1771.mp3", creationcollisionoption.replaceexisting); using(varstream =awaitfile. Openstreamforwriteasync ()) {awaitGetdata.copytoasync (stream);//The stream stream that will get the HttpClient stream copied to the fileawaitStream. Flushasync ();//Asynchronous Flush}returnThis_voice_files; } }
UWP:
Public AsyncTask<storagefile>Load () {Try { varHttpClient =NewHttpClient (); varBuffer =awaitHttpclient.getbufferasync (NewUri ("Http://dx.sc.chinaz.com/Files/DownLoad/sound1/201207/1771.mp3")); if(Buffer! =NULL&& buffer. Length >0u) { varFile =awaitApplicationData.Current.LocalCacheFolder.CreateFileAsync ("1771.mp3", creationcollisionoption.replaceexisting); using(varstream =awaitfile. OpenAsync (Fileaccessmode.readwrite)) {awaitStream. WriteAsync (buffer); awaitStream. Flushasync (); } returnfile; } } Catch { } return NULL;}
C#:
HttpClient client =NewHttpClient (); using(varMusicstream = client. Getstreamasync ("Http://dx.sc.chinaz.com/Files/DownLoad/sound1/201207/1771.mp3"). Result) {intCurrentpos =0; byte[] Tempbyte =New byte[4096]; MemoryStream Ms=NewMemoryStream (4096); //when you hit 0, you read it. 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"); }
How WP8.1 and UWP use the audio on the download page and save