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 方法的文档,当流参数为空时,它看起来会引发参数异常。我的建议是尝试编写集成测试或编写一些防御性代码来尝试解决问题。

例如

集成测试:

[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 回答