OpenAL iPhone:无法播放任何声音
我试图通过参考 本教程:
我已经从中创建了一个整体代码来进行初步测试,但无法播放声音。我已经尝试了很多东西,但无法播放声音。非常感谢任何帮助。 谢谢。
这是我的代码:
ALCdevice* device;
device = alcOpenDevice(NULL);
ALCcontext* context;
alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
NSString* path = [[NSBundle mainBundle] pathForResource:@"mg" ofType:@"caf"];
NSURL* pathURL = [NSURL fileURLWithPath:path];
AudioFileID audioID;
OSStatus audioStatus = AudioFileOpenURL((CFURLRef)pathURL, kAudioFileReadPermission, 0, &audioID);
UInt32 fileSize = 0;
UInt64 outDataSize;
UInt32 propSize = sizeof(UInt64);
OSStatus res = AudioFileGetProperty(audioID, kAudioFilePropertyAudioDataByteCount, &propSize, &outDataSize);
fileSize = (UInt32)outDataSize;
unsigned char* outData = malloc(fileSize);
OSStatus res2 = AudioFileReadBytes(audioID, false, 0, &fileSize, outData);
AudioFileClose(audioID);
ALuint bufferID;
alGenBuffers(1, &bufferID);
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, fileSize, 44100);
ALuint sourceID = 2;
alGenSources(1, &sourceID);
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
alSourcei(sourceID, AL_LOOPING, AL_FALSE);
free(outData);
outData = NULL;
alSourcePlay(sourceID);
I'm trying to get to play a simple sound file through OpenAL by referring this tutorial:
I've created a monolithic code from it to test initially, but cant get the sound to play. I've been trying a lot of stuff but I can't get the sound to play. Any help is much appreciated.
Thanks.
Here is my code:
ALCdevice* device;
device = alcOpenDevice(NULL);
ALCcontext* context;
alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
NSString* path = [[NSBundle mainBundle] pathForResource:@"mg" ofType:@"caf"];
NSURL* pathURL = [NSURL fileURLWithPath:path];
AudioFileID audioID;
OSStatus audioStatus = AudioFileOpenURL((CFURLRef)pathURL, kAudioFileReadPermission, 0, &audioID);
UInt32 fileSize = 0;
UInt64 outDataSize;
UInt32 propSize = sizeof(UInt64);
OSStatus res = AudioFileGetProperty(audioID, kAudioFilePropertyAudioDataByteCount, &propSize, &outDataSize);
fileSize = (UInt32)outDataSize;
unsigned char* outData = malloc(fileSize);
OSStatus res2 = AudioFileReadBytes(audioID, false, 0, &fileSize, outData);
AudioFileClose(audioID);
ALuint bufferID;
alGenBuffers(1, &bufferID);
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, fileSize, 44100);
ALuint sourceID = 2;
alGenSources(1, &sourceID);
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
alSourcei(sourceID, AL_LOOPING, AL_FALSE);
free(outData);
outData = NULL;
alSourcePlay(sourceID);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚发现!
我一直在使用:
而不是:
alGetError() 有助于指出这一点。
Just found out!
I have been using:
instead of:
The alGetError() helped in pointing this out.