我在使用 OpenAL 加载声音时遇到问题:
// in SoundManager.cs
public void LoadSound(string soundId, string path)
{
// Generate a buffer.
int buffer = -1;
Al.alGenBuffers(1, out buffer);
int errorCode = Al.alGetError();
System.Diagnostics.Debug.Assert(errorCode == Al.AL_NO_ERROR);
int format;
float frequency;
int size;
System.Diagnostics.Debug.Assert(File.Exists(path));
IntPtr data = Alut.alutLoadMemoryFromFile(path, out format, out size,
out frequency);
System.Diagnostics.Debug.Assert(data != IntPtr.Zero, "Problem");
// Load wav data into the generated buffer.
Al.alBufferData(buffer, format, data, size, (int)frequency);
// Everything seems ok, add it to the library.
_soundIdentifier.Add(soundId, new SoundSource(buffer, path));
}
// Form.cs
private void InitializeSounds()
{
_soundManager.LoadSound("effect", "soundA.wav");
}
soundIdentifier 是一个字典,在 SoundSource 中我保存声音的信息,第一个字符串是声音的正常名称,如“cow”、“horse”或其他。
我从 Form.cs 调用 InitializeSounds,而 LoadSound 是声音管理器的方法。
Alut.alutLoadMemoryFromFile 导致错误,由于某种原因返回一个空指针。
剩下的就是简单的代码希望你能看懂。
我使用 Tao.OpenAL 在 c# 中工作。