9

分離ストレージから選択した mp3 ファイルを再生するための次のコードで、上記の例外とエラーが発生します。

using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{            
     using (var isfs = isf.OpenFile(selected.Path, FileMode.Open))
     {                        
          this.media.SetSource(isfs);              
          isfs.Close();                        
     }                    
     isf.Dispose();
}

エラーは非常に漠然としているので、何が間違っているのかよくわかりません...潜在的にチェックできるこのエラーのアイデアまたは少なくとも一般的な原因はありますか?

編集:例外がスローされています:using(var isfs = isf.OpenFile(...))

編集 2: スタック トレース...

at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, IsolatedStorageFile isf)
at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, IsolatedStorageFile isf)
at Ringify.Phone.PivotContent.RingtoneCollectionPage.MediaIconSelected(Object sender, GestureEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

1 つの曲を再生してから停止し (UI に再生と一時停止のボタンがあります)、別の曲を再生しても、エラーは発生しないことにも気付きました。ある曲を再生して停止し、同じ曲をもう一度再生しようとすると発生します。

4

2 に答える 2

8

同じ音楽を 2 回再生すると問題が発生するため、ファイル共有の問題である可能性があります。OpenFileメソッドの FileShare パラメータを提供するようにしてください。

var isfs = isf.OpenFile(selected.Path, FileMode.Open, FileShare.Read)

ファイルを明示的に閉じているため、なぜそれが起こるのかわかりません。

編集: OK、リフレクターで掘り下げて、私はそれを理解しました。MediaElement.SetSource のコードは次のとおりです。

public void SetSource(Stream stream)
{
    if (stream == null)
    {
        throw new ArgumentNullException("stream");
    }
    if (stream.GetType() != typeof(IsolatedStorageFileStream))
    {
        throw new NotSupportedException("Stream must be of type IsolatedStorageFileStream");
    }
    IsolatedStorageFileStream stream2 = stream as IsolatedStorageFileStream;
    stream2.Flush();
    stream2.Close();
    this.Source = new Uri(stream2.Name, UriKind.Absolute);
}

したがって、基本的には、指定したストリームを使用せず、閉じます。しかし、それはファイルの名前を保持し、音楽を再生すると再び開くと思います。したがって、音楽の再生中に排他アクセスで同じファイルを再度開こうとすると、MediaElement でファイルが開かれているため失敗します。トリッキー。

于 2012-05-01T21:43:08.590 に答える
1

IsolatedStorageFileStreamを使用する必要があると思います:

using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{            
     using (var isfs = new IsolatedStorageFileStream(selected.Path, FileMode.Open, isf))
     {                        
          this.media.SetSource(isfs);              
     }                    
}

.Close()また、メソッドまたは.Dispose()メソッドは us​​ing ステートメントで処理されるため、呼び出す必要がないことに注意してください。

于 2012-05-01T20:59:21.603 に答える