如何从uri下载音频文件并保存在Windows Phone 8的本地文件夹中?
问题描述:
我见过这类问题,但没有一个符合我的要求。我的服务器里有各种类型的铃声。现在我需要知道如何读取这些音频文件并保存到我的Windows Phone 8目录中。请帮我;我在等你宝贵的建议。先谢谢
I've seen this type of question but none of those goes to my requirements. I've got various types of ringtone in my server. now i need to know how to read those audio file and save into my windows phone 8 directory. Please help me; I'm waiting for your valuable suggestion. Thanks in Advance
答
//////下载并保存
private void addButton_Click(object sender,RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.OpenReadAsync(new Uri(https://dl.dropboxusercontent.com/u/65265347/dog38catsi_a2yn44rm.mp3,UriKind.RelativeOrAbsolute));
webClient.OpenReadCompleted + = webClient_OpenReadCompleted;
}
无效webClient_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e)
{
string fileName =createmytone.mp3;
Debug.WriteLine(e.Result) ;
使用(IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if(myIsolatedStorage.FileExists(fileName))
{
myIsolatedStorage.DeleteFile(fileName);
}
使用(IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName,FileMode.Create,myIsolatedStorage))
{
using( BinaryWriter writer = new BinaryWriter(fileStream))
{
Stream resourceStream = e.Result as Stream;
//streamResourceInfo.Stream;
long length = resourceStream.Length;
byte [] buffer = new byte [32];
int readCount = 0;
使用(BinaryReader reader = new BinaryReader(resourceStream))
{
//以块的形式读取文件以减少内存消耗并提高性能
while(readCount< length)
{
int actual = reader.Read(buffer,0,buffer.Length);
readCount + = actual;
writer.Write(buffer,0,actual);
}
}
}
}
}
}
////设置为铃声
SaveRingtoneTask saveRingtoneTask = null;
private void setRingtoneButton_Click(object sender,RoutedEventArgs e)
{
尝试
{
string isoStorePath = string.Concat(isostore:/,createmytone.mp3 );
saveRingtoneTask.Source = new Uri(isoStorePath);
saveRingtoneTask.DisplayName =我的铃声......;
saveRingtoneTask .Show();
}
catch(例外情况)
{
MessageBox.Show(例如消息);
}
}
////// For Download and save
private void addButton_Click(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.OpenReadAsync(new Uri("https://dl.dropboxusercontent.com/u/65265347/dog38catsi_a2yn44rm.mp3", UriKind.RelativeOrAbsolute));
webClient.OpenReadCompleted+=webClient_OpenReadCompleted;
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
string fileName = "createmytone.mp3";
Debug.WriteLine(e.Result);
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(fileName))
{
myIsolatedStorage.DeleteFile(fileName);
}
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, myIsolatedStorage))
{
using (BinaryWriter writer = new BinaryWriter(fileStream))
{
Stream resourceStream = e.Result as Stream;
//streamResourceInfo.Stream;
long length = resourceStream.Length;
byte[] buffer = new byte[32];
int readCount = 0;
using (BinaryReader reader = new BinaryReader(resourceStream))
{
// 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);
}
}
}
}
}
}
//// Set as Ringtone
SaveRingtoneTask saveRingtoneTask = null;
private void setRingtoneButton_Click(object sender, RoutedEventArgs e)
{
try
{
string isoStorePath = string.Concat("isostore:/", "createmytone.mp3");
saveRingtoneTask.Source = new Uri(isoStorePath);
saveRingtoneTask.DisplayName = "My Ringtone...";
saveRingtoneTask.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}