【问题标题】:how to map StreamResourceinfo to a absolute URL如何将 StreamResourceinfo 映射到绝对 URL
【发布时间】:2013-06-26 13:52:08
【问题描述】:

我正在使用以下片段将音频文件保存在隔离存储中。但是当 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);
                        }
                    }
                }
            }
        }
    }

提前致谢。

【问题讨论】:

    标签: silverlight windows-phone-7 windows-phone-8 isolatedstorage


    【解决方案1】:

    您不能使用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"));
    

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 2019-05-27
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多