我發現StackOverflow在過去相當有幫助,現在我已經碰到了我自己的一個障礙,以爲我會嘗試在這裏發帖。解決XNA內容加載器InvalidOperationException?
我的問題是,每當我的遊戲嘗試加載SoundEffect文件時,它都會崩潰,併發生InvalidOperationException(詳細消息:發生意外錯誤)。這個遊戲是在XNA 4.0框架上編寫的,用Visual Studio 2010 Express作爲我的IDE。加載的聲音效果都是.wav文件,並添加到遊戲的內容項目中。
我檢查了董事會和嘗試了以下建議:
- 確認content.rootDirectory它設置 - 它被設置爲「內容」
- 確認content.load <>通過訪問資源正確的道路。使用反射,我得到應用程序的當前目錄,然後使用root目錄+它嘗試訪問的路徑。該文件完全存在,可以訪問,並且位於該位置。
- 確保ContentManager內容不爲空 - 使用調試進行確認,並且無論如何都會拋出不同的異常。
- 確認WAV位於8位或16位PCM中。證實。
- 可能的頭損壞?這個錯誤發生在我嘗試加載的任何和所有聲音效果上,所以它不是與一個文件有關的標題問題。
奇怪的是,這個錯誤似乎已經不知從何而來。它在過去的一週沒有問題,今天它出了問題 - 我沒有做任何會影響內容加載過程的改變。
下面是引發錯誤的代碼:
public void LoadSoundEffect(ContentManager content, String assetPath)
{
if (content != null && String.IsNullOrEmpty(assetPath) == false)
{
// This next line throws the exception.
SoundEffect effectLoader = content.Load<SoundEffect>(assetPath);
soundLibrary.Add(assetPath, effectLoader);
}
}
這裏的堆棧跟蹤:提前爲你能提供任何幫助
at Microsoft.Xna.Framework.Helpers.ThrowExceptionFromErrorCode(Int32 error) at Microsoft.Xna.Framework.Audio.SoundEffect.AllocateFormatAndData(Byte[] format, Byte[] > data, Int32 offset, Int32 count) at Microsoft.Xna.Framework.Audio.SoundEffect.Create(Byte[] format, Byte[] data, Int32 offset, Int32 count, Int32 loopStart, Int32 loopLength, TimeSpan duration) at Microsoft.Xna.Framework.Audio.SoundEffect..ctor(Byte[] format, Byte[] data, Int32 loopStart, Int32 loopLength, TimeSpan duration) at Microsoft.Xna.Framework.Content.SoundEffectReader.Read(ContentReader input, SoundEffect existingInstance) at Microsoft.Xna.Framework.Content.ContentReader.InvokeReader[T](ContentTypeReader reader, Object existingInstance) at Microsoft.Xna.Framework.Content.ContentReader.ReadObjectInternal[T](Object existingInstance) at Microsoft.Xna.Framework.Content.ContentReader.ReadObjectT at Microsoft.Xna.Framework.Content.ContentReader.ReadAssetT at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject) at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName) at SerializableDataTypes.AudioManager.LoadSoundEffect(ContentManager content, String assetPath) in C:\Users\Mike\Documents\Visual Studio 2010\Projects\Res Judicata Chapter1\SerializableDataTypes\Script Elements\AudioManager.cs:line 78
感謝 - 我對這個完全難倒,這是一個真令人沮喪的錯誤。
當然,我可以試試。我似乎已經通過重新啓動我的系統來實現它(現在),但是當我有替代音頻運行時,它仍然會隨着該消息而崩潰。我會看到在修復後我是否可以重新創建該問題! – 2013-04-11 01:00:52