【问题标题】:Cannot save song from isolated storage, SaveSong method throws InvalidOperationException error无法从隔离存储中保存歌曲,SaveSong 方法抛出 InvalidOperationException 错误
【发布时间】:2014-08-06 03:57:30
【问题描述】:

我正在为 Windows Phone 8 构建一个 FTP 应用程序,并希望将下载的歌曲从独立存储中保存到媒体库。我检查文件是否存在于 isostore 中,它返回 true,但是当我使用 SaveSong 方法时,它总是抛出异常。这是代码示例:

  private async void contextMenuItem1_Click(object sender, RoutedEventArgs e)
    {
        string fileName = (sender as MenuItem).DataContext.ToString();
        MediaLibrary library = null;

        ......

        else if (fileName.EndsWith(".mp3") || fileName.EndsWith(".wav") || fileName.EndsWith(".aac"))
        {                
           IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

                if (myIsolatedStorage.FileExists(fileName))
                {
                    library = new MediaLibrary();
                    StorageFile localFile = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

                    if (localFile != null)
                    {
                        //MessageBox.Show("StorageFile is: " + localFile.Name);
                        try
                        {
                            library.SaveSong(new Uri(localFile.Name, UriKind.RelativeOrAbsolute), null, SaveSongOperation.CopyToLibrary);
                            //MediaLibraryExtensions.SaveSong(media, new Uri(fileName, UriKind.RelativeOrAbsolute), null, SaveSongOperation.CopyToLibrary);
                        }

                        catch (InvalidOperationException ex)
                        {
                            MessageBox.Show("Exception caught: " + ex.Message);
                        }
                    }
                }

                else
                    MessageBox.Show("File does not exist in isostore");         
        }
    }

如果有人可以帮助我,我将非常感激,谢谢。

【问题讨论】:

    标签: c# windows-phone-8 isolatedstorage invalidoperationexception media-library


    【解决方案1】:

    如果您的文件名或文件路径为空,则出现此异常。另外请验证是否添加了ID_CAP_MEDIALIB_AUDIO 功能。

    FYI

    【讨论】:

    • 我找到了解决方案。似乎文件的流没有关闭,所以我遇到了这些问题。有时我可以保存这首歌,有时我不能。关闭文件流后,它似乎没有任何问题
    • IBuffer databuffer = dataReader.DetachBuffer(); RaiseFtpFileTransferProgressedEvent(databuffer.Length, false); await ftpFileInfo.LocalFileStream.WriteAsync(databuffer.ToArray(), 0, Convert.ToInt32(databuffer.Length)); } await ftpFileInfo.LocalFileStream.FlushAsync(); ftpFileInfo.LocalFileStream.Close(); dataReader.Dispose(); dataReader = null;
    猜你喜欢
    • 2019-07-17
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多