2012-12-12 69 views
0

我試圖播放使用Microsoft.Xna.Framework的wav文件,但我無法解決此錯誤。在Microsoft.Xna.Framework.ni.dll中發生類型'System.ArgumentException'的異常

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(); 
     } 
+1

哪條線拋出該異常?是否有內部異常?請發佈完整的堆棧跟蹤。聲音文件是否正確加載?請注意,SoundEffect只能處理PCM音頻8或16位,8KHz至48KHz,單聲道或立體聲。您的wav文件是否兼容(http://blogs.msdn.com/b/ashtat/archive/2010/06/03/soundeffect-creation-in-xna-game-studio-4.aspx)? – Ani

+0

謝謝,這是完整的堆棧跟蹤,因爲它的第一次機會例外。我認爲問題是我的wav可能不是PCM。有什麼地方可以找到一個PCM音頻文件來測試嗎? – joe

+0

XNA示例(http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/getting_started)? – Ani

回答

0

按照documentation的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); 
} 
+0

謝謝,錯誤發生在行TitleContainer.OpenStream(dingSoundFile)所以stream!= null將無法到達。 – joe

+0

看起來問題在於傳遞聲音文件的路徑,因爲它無法找到或不存在。您可能希望圍繞您的使用陳述進行嘗試/捕捉。 – Flea

+0

此線程似乎表明與嘗試訪問聲音文件的相對路徑類似的問題。 (http://xboxforums.create.msdn.com/forums/p/51049/308573.aspx)。我認爲try/catch可能有助於縮小錯誤(至少對於調試)。這也表明,以斜線領先不再有效。你可能想嘗試從你的路徑中刪除它。 – Flea

相關問題