【问题标题】:Exception of type 'System.ArgumentException' occurred in Microsoft.Xna.Framework.ni.dllMicrosoft.Xna.Framework.ni.dll 中出现“System.ArgumentException”类型的异常
【发布时间】:2012-12-12 15:52:01
【问题描述】:

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

【问题讨论】:

  • 哪一行会抛出这个异常?有内部异常吗?请发布完整的堆栈跟踪。声音文件是否正确加载?请注意,SoundEffect 只能处理 8 位或 16 位、8KHz 至 48KHz、单声道或立体声的 PCM 音频。您的 wav 文件是否兼容 (blogs.msdn.com/b/ashtat/archive/2010/06/03/…)?
  • 谢谢,这是完整的堆栈跟踪,因为它是第一次机会异常。我认为问题在于我的 wav 可能不是 PCM。有什么地方可以找到一个示例 PCM 音频文件进行测试?
  • 验证了 PCM,还是有问题。

标签: windows-phone-7 xna windows-phone windows-phone-8


【解决方案1】:

根据 SoundEffect.FromStream 方法的documentation,当流参数为 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);
}

【讨论】:

  • 谢谢,错误发生在 TitleContainer.OpenStream(dingSoundFile) 行,因此无法到达 stream != null。
  • 似乎问题在于传入的声音文件的路径无法找到或不存在。你可能想在你的 using 语句周围加上一个 try/catch。
  • 这个线程似乎表明在尝试访问声音文件的相对路径时遇到了类似的问题。 (xboxforums.create.msdn.com/forums/p/51049/308573.aspx)。我认为 try/catch 可能有助于缩小错误范围(至少对于调试而言)。它还表明以斜线开头不再有效。您可能想尝试从路径中删除它。
猜你喜欢
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多