Windows phone 7 类型'的例外情况;System.ArgumentException';发生在Microsoft.Xna.Framework.ni.dll中

Windows phone 7 类型'的例外情况;System.ArgumentException';发生在Microsoft.Xna.Framework.ni.dll中,windows-phone-7,xna,windows-phone,windows-phone-8,Windows Phone 7,Xna,Windows Phone,Windows Phone 8,我正在尝试使用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 n

我正在尝试使用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)

根据SoundEffect.FromStream方法的说明,当stream参数为null时,它似乎会引发参数异常。我的建议是尝试编写一个集成测试或编写一些防御代码来尝试解决这个问题

e、 g

集成测试:

[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);
}

防御性编码:

[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);
}

哪一行抛出此异常?是否存在内部异常?请发布完整的堆栈跟踪。声音文件加载是否正确?请注意,SoundEffect只能处理PCM音频8或16位、8KHz至48KHz、单声道或立体声。您的wav文件兼容吗()?谢谢,这是完整的堆栈跟踪,因为这是第一次出现异常。我认为问题在于我的wav可能不是PCM。是否有任何地方可以找到示例PCM音频文件进行测试?XNA示例()?验证了PCM,仍然存在问题。谢谢,错误发生在TitleContainer.OpenStream(dingSoundFile)so stream!=无法达到null。似乎问题在于传入的声音文件路径无法找到或不存在。您可能希望在using语句周围添加一个try/catch。此线程似乎表明在尝试访问声音文件的相对路径时存在类似问题。(). 我认为try/catch可能有助于缩小错误范围(至少对于调试而言)。它还表明用斜杠引导不再有效。您可能希望尝试从路径中删除该选项。