我能够将媒体文件存储到隔离存储中,并能够检索这些文件。现在我想要的是在电话中播放音乐播放器中的文件,但是音乐播放器说“找不到音乐”。如何将在隔离存储中下载的mp3文件显示给媒体库?我使用这段代码下载mp3文件。有没有办法将文件下载到可读的存储器(电话内存/内部存储/存储卡)?
void imgDown_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ToastPrompt toast = new ToastPrompt();
toast.Foreground = new SolidColorBrush(Colors.Black);
toast.FontSize = 20;
toast.Background = new SolidColorBrush(Colors.Yellow);
toast.Message = "Please wait";
toast.Show();
try
{
//Create a webclient that'll handle your download
WebClient client = new WebClient();
//Run function when resource-read (OpenRead) operation is completed
client.OpenReadCompleted += client_OpenReadCompleted;
//Start download / open stream
client.OpenReadAsync(new Uri(streamUrl));
}
catch (Exception ex)
{
toast.Message = "Downloading error";
toast.Show();
}
}
async void client_OpenReadCompleted(object sender,
OpenReadCompletedEventArgs e)
{
ToastPrompt toast = new ToastPrompt();
toast.Foreground = new SolidColorBrush(Colors.Black);
toast.FontSize = 20;
toast.Background = new SolidColorBrush(Colors.Yellow);
toast.Message = "Downloading starts";
toast.Show();
try
{
byte[] buffer = new byte[e.Result.Length];
//Store bytes in buffer, so it can be saved later on
await e.Result.ReadAsync(buffer, 0, buffer.Length);
using (IsolatedStorageFile file
IsolatedStorageFile.GetUserStoreForApplication())
{
//Create file
using (IsolatedStorageFileStream stream =
file.OpenFile(titleUrl+".mp3", FileMode.Create))
{
//Write content stream to file
await stream.WriteAsync(buffer, 0, buffer.Length);
}
// StorageFolder local =
Windows.Storage.ApplicationData.Current.LocalFolder;
//StorageFile storedFile = await local.GetFileAsync(titleUrl +
".mp3");
toast.Message = "Downloading complete";
toast.Show();
}
catch (Exception ex)
{
toast.Message = "We could not finish your download. Error ";
toast.Show();
}发布于 2015-08-06 16:00:23
看起来您需要使用MediaLibraryExtensions.SaveSong方法(参见https://msdn.microsoft.com/library/microsoft.xna.framework.media.PhoneExtensions.medialibraryextensions.savesong(v=xnagamestudio.42).aspx )
您需要将ID_CAP_MEDIALIB_AUDIO功能添加到应用程序中,并为隔离存储上的文件获取一个URI以传递给该方法。
在MSDN上有一些关于Windows数据的更多信息。
https://stackoverflow.com/questions/31860250
复制相似问题