0

Microsoft.Xna.Framework を使用して wav ファイルを再生しようとしていますが、このエラーを解決できません。

A first chance exception of type 'System.ArgumentException' occurred in Microsoft.Xna.Framework.ni.dll
An exception of type 'System.ArgumentException' occurred in Microsoft.Xna.Framework.ni.dll but was not handled in user code

以下は私のコードです:(エラーは行で発生しますTitleContainer.OpenStream(dingSoundFile):)

        SoundEffectInstance seiCircus;
        string dingSoundFile = "/Html/sounds/tap.wav";
        using (var stream = TitleContainer.OpenStream(dingSoundFile))
        {
            var effect = SoundEffect.FromStream(stream);
            //create the instance
            seiCircus = effect.CreateInstance();

            FrameworkDispatcher.Update();
            //play sound via the instance
            seiCircus.Play();
        }
4

1 に答える 1

0

SoundEffect.FromStreamメソッドのドキュメントによると、ストリームパラメータがnullの場合、引数例外をスローするようです。私の推奨事項は、統合テストを作成するか、問題を解決するための防御コードを作成することです。

例えば

統合テスト:

[Test]
public void Test() {
    string dingSoundFile = "Html/sounds/tap.wav"; //NOTE: Remove leading slash
    try {
       var stream = TitleContainer.OpenStream(dingSoundFile);
       Assert.IsNotNull(stream); 
    } catch (Exception ex) {
       Assert.Fail(ex.Message);
    }
}

また

防御的なコーディング:

SoundEffectInstance seiCircus;
string dingSoundFile = "Html/sounds/tap.wav"; //NOTE: Remove leading slash
try {
    using (var stream = TitleContainer.OpenStream(dingSoundFile)) {
        if(stream != null) {
           var effect = SoundEffect.FromStream(stream);
           seiCircus = effect.CreateInstance();

           FrameworkDispatcher.Update();
           seiCircus.Play();
        }
    }
} catch (Exception ex) {
      Debug.WriteLine(ex.Message);
}
于 2012-12-12T16:50:14.960 に答える