将 mp3 文件从独立存储复制到媒体库 windows phone 8
Copy mp3 files from Isolated storage to media library in windows phone 8
我能够将媒体文件存储到独立存储中并能够检索它们。现在我想要的是在 phone 的音乐播放器中播放该文件,但音乐播放器显示 "no music found"。如何将我下载的独立存储中的 mp3 文件显示到媒体库?我正在使用此代码下载 mp3 文件。有什么方法可以将文件下载到可读存储,如(phone内存/内部存储/存储卡)???
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();
}
看来您需要使用 MediaLibraryExtensions.SaveSong
方法(参见 https://msdn.microsoft.com/library/microsoft.xna.framework.media.PhoneExtensions.medialibraryextensions.savesong(v=xnagamestudio.42).aspx)
您需要将 ID_CAP_MEDIALIB_AUDIO
功能添加到您的应用,并获取独立存储上文件的 URI 以传递给该方法。
在 MSDN 上有一些关于 Data for Windows Phone 的更多信息。
我能够将媒体文件存储到独立存储中并能够检索它们。现在我想要的是在 phone 的音乐播放器中播放该文件,但音乐播放器显示 "no music found"。如何将我下载的独立存储中的 mp3 文件显示到媒体库?我正在使用此代码下载 mp3 文件。有什么方法可以将文件下载到可读存储,如(phone内存/内部存储/存储卡)???
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();
}
看来您需要使用 MediaLibraryExtensions.SaveSong
方法(参见 https://msdn.microsoft.com/library/microsoft.xna.framework.media.PhoneExtensions.medialibraryextensions.savesong(v=xnagamestudio.42).aspx)
您需要将 ID_CAP_MEDIALIB_AUDIO
功能添加到您的应用,并获取独立存储上文件的 URI 以传递给该方法。
在 MSDN 上有一些关于 Data for Windows Phone 的更多信息。