0

我正在使用以下片段将音频文件保存在隔离存储中。但是当 streamresourceinfo 映射到 absoluteUri 时会发生异常。uri 只接受相对 uri。请指导我如何使用绝对 Uri 保存音频文件。

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);
                        }
                    }
                }
            }
        }
    }

提前致谢。

4

1 回答 1

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"));
于 2013-06-26T14:07:52.357 回答