我发现 StackOverflow 过去是一个非常有用的引用,现在我遇到了自己的障碍,我想我会尝试在这里发帖。
我的问题是,每当我的游戏尝试加载 SoundEffect 文件时,它都会崩溃并出现 InvalidOperationException(详细消息:发生意外错误)。这个游戏是在 XNA 4.0 框架上用 C# 编写的,我的 IDE 是 Visual Studio 2010 express。加载的音效都是.wav文件,添加到游戏的Content工程中。
我检查了板子并尝试了以下建议:
- 确认它设置的 content.rootDirectory - 它被设置为“内容”
- 确认 content.load<> 正在通过正确的路径访问资源。使用反射,我得到了应用程序的当前目录,然后使用了根目录+它试图访问的路径。该文件肯定存在、可访问并且位于该位置。
- 确保 ContentManager 内容不为空 - 使用调试确认,无论如何都会抛出不同的异常。
- 确认 WAV 是 8 位或 16 位 PCM。确认。
- 标题可能损坏?此错误发生在我尝试加载的任何和所有音效上,因此它不是与一个文件有关的 header 问题。
奇怪的是,这个错误似乎不知从何而来。在过去的一周里,它一直没有问题,今天却出了问题——我没有做任何会影响内容加载过程的更改。
这是抛出错误的代码:
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
预先感谢您提供的任何帮助 - 我对此完全感到困惑,这是一个非常令人沮丧的错误。
最佳答案
出于好奇,您能否尝试以稍微不同的方式加载您的音效。
SoundEffect effect = content.Load<SoundEffect>(String.Format(@"{0}", soundEffectPath))
我过去在不使用字符串文字时加载某些内容类型时遇到过问题。
关于c# - 解决 XNA Content Loader InvalidOperationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15261323/