【问题标题】:Copy mp3 files from Isolated storage to media library in windows phone 8将 mp3 文件从独立存储复制到 windows phone 8 中的媒体库
【发布时间】: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


【解决方案1】:

看来您需要使用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 的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多