我正在尝试使用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();
}
答案 0 :(得分:0)
根据SoundEffect.FromStream方法的documentation,当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);
}