2012-02-14 54 views
1

我试图下载并保存从互联网.mp3文件,但卡住了与流从外部链接:如何在Windows Phone 7上从互联网上保存MP3文件?

private void saveSound() 
    { 
     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); 
     using (var fs = new IsolatedStorageFileStream("123.mp3", FileMode.Create, iso)) 
     { 
      //Here should be this Stream from the Internet... 
      //Uri: "http://my-site.com/mega-popular-song.mp3" 
      StreamResourceInfo rs = new StreamResourceInfo(stream, "audio/mpeg"); 
      int count = 0; 
      byte[] buffer = new byte[4096]; 
      while (0 < (count = rs.Stream.Read(buffer, 0, buffer.Length))) 
      { 
       fs.Write(buffer, 0, count); 
      } 
      fs.Close(); 

     } 
    } 

应此流的是什么样的?下载和保存.mp3文件的最佳方法是什么?

+1

我相信你需要从“webClient”对象和Complete事件处理程序的参数中获取流。 – Bob 2012-02-15 11:11:52

回答

1

我敢肯定this article让你在那里。像Bob提到的那样,您必须使用WebClient。基本上这是代码的神奇:

wc.OpenReadCompleted += ((s, args) => 
{ 
    using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     if (store.FileExists(fileName)) 
      store.DeleteFile(fileName); 

     using (var fs = new IsolatedStorageFileStream(fileName, FileMode.Create, store)) 
     { 
      byte[] bytesInStream = new byte[args.Result.Length]; 
      args.Result.Read(bytesInStream, 0, (int)bytesInStream.Length); 
      fs.Write(bytesInStream, 0, bytesInStream.Length); 
      fs.Flush(); 
     } 
    } 
}); 

但我会阅读完整的文章,以充分理解发生了什么。希望这可以帮助!