【发布时间】:2015-08-06 15:47:56
【问题描述】:
我能够将媒体文件存储到独立存储中并能够检索它们。现在我想要的是在手机的音乐播放器中播放该文件,但音乐播放器显示“找不到音乐”。如何将我下载的独立存储中的 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();
}
【问题讨论】:
-
获得 StorageFile storedFile 后,您是否尝试将其复制到 MusicFolder:
await storedFile.CopyAsync(KnownFolders.MusicLibrary);?当然,为此,您需要清单中的 Music Library 功能。
标签: c# windows-phone-8.1 isolatedstorage