我使用下面的剪辑将音频文件保存在独立存储中。但是当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);
}
}
}
}
}
}
提前致谢。
答案 0 :(得分: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"));