0

我正在使用以下剪切方式將音頻文件保存在獨立存儲中。但當streamresourceinfo映射到absoluteUri時發生異常。 uri只接受相對的uri。請指導我如何使用絕對Uri保存音頻文件。如何將StreamResourceinfo映射到絕對URL

private void SaveMp3() 
    { 
     string FileName = "Audios/Deer short.mp3"; 
     FileName = "http://www.ugunaflutes.co.uk/Deer short.mp3"; 
     StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(FileName, UriKind.RelativeOrAbsolute)); 

     using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      if (myIsolatedStorage.FileExists(FileName)) 
      { 
       myIsolatedStorage.DeleteFile(FileName); 
      } 

      using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("Audio.png", FileMode.Create, myIsolatedStorage)) 
      { 
       using (BinaryWriter writer = new BinaryWriter(fileStream)) 
       { 
        Stream resourceStream = streamResourceInfo.Stream; 
        long length = resourceStream.Length; 
        byte[] buffer = new byte[32]; 
        int readCount = 0; 
        using (BinaryReader reader = new BinaryReader(streamResourceInfo.Stream)) 
        { 
         // read file in chunks in order to reduce memory consumption and increase performance 
         while (readCount < length) 
         { 
          int actual = reader.Read(buffer, 0, buffer.Length); 
          readCount += actual; 
          writer.Write(buffer, 0, actual); 
         } 
        } 
       } 
      } 
     } 
    } 

在此先感謝。

回答

0

您不能使用Application.GetResourceStream來加載外部資源,因爲URI必須是相對於應用程序包http://msdn.microsoft.com/en-us/library/ms596994(v=vs.95).aspx。 您需要使用WebClient.OpenReadAsync下載您的mp3文件,並在本地保存到IsolatedStorage後,舉例如下:

var webClient = new WebClient(); 
      webClient.OpenReadCompleted += (sender, args) => 
               { 
                if (args.Error != null) 
                { 
                 //save file here 
                } 
               }; 

      webClient.OpenReadAsync(new Uri("http://www.ugunaflutes.co.uk/Deer short.mp3"));