iOS SDK 4.3 OpenAL alGenSources 导致 AL_INVALID_OPERATION
我正在尝试掌握 OpenAL,通过这里的教程进行操作: http://benbritten.com/2008/11/06/openal-sound-on-the-iphone/
我的问题是声音无法播放,尽管没有抛出任何 iOS 错误。但存在 OpenAL 错误。下面的代码示例是 IBAction 方法的主体,并在 alGenSources(1, &sourceID) 处产生 AL_INVALID_OPERATION。 sourceID 报告为 NULL。
我已经在设备和模拟器上尝试过了。
该代码示例似乎使用相当广泛,但我找不到任何人抱怨这个特定问题。有人能解释一下吗?非常感谢您的帮助,
NSString *audioFileName = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"caf"];
AudioFileID fileID = [self openAudioFile:audioFileName];
UInt32 filesize = [self audioFileSize:fileID];
unsigned char *outData = malloc(filesize);
OSStatus result = noErr;
result = AudioFileReadBytes(fileID, false, 0, &filesize, outData);
AudioFileClose(fileID);
if (result != 0) {
NSLog(@"Can't load file..");
}
NSUInteger bufferID;
//NSLog(@"bufferID %@", [NSNumber numberWithUnsignedInteger:bufferID]);
alGenBuffers(1, &bufferID);
//NSLog(@"bufferID %@", [NSNumber numberWithUnsignedInteger:bufferID]);
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, filesize, 44100);
[bufferStorageArray addObject:[NSNumber numberWithUnsignedInteger:bufferID]];
alGetError();
ALuint sourceID;
alGenSources(1, &sourceID);
if(alGetError() == AL_INVALID_OPERATION)
{
printf("\n++++ Error creating buffers INVALID_OPERATION!!\n");
//exit(1);
}
else
{
printf("No errors yet.");
}
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
if (loops) {
alSourcei(sourceID, AL_LOOPING, AL_TRUE);
}
[soundDictionary setObject:[NSNumber numberWithUnsignedInt:sourceID] forKey:@"sound"];
if (outData) {
free(outData);
outData = NULL;
}
[self playSound:@"sound"];
I'm trying to get to grips with OpenAL, working through a tutorial here: http://benbritten.com/2008/11/06/openal-sound-on-the-iphone/
My problem is that the sound does not play, although there are no iOS errors thrown. There is an OpenAL error though. The code sample below is the body of an IBAction method, and results in an AL_INVALID_OPERATION at alGenSources(1, &sourceID). sourceID reports as NULL.
I've tried this on the device and the simulator.
This code sample seems to be in pretty wide use, but I can't find anybody complaining of this particular problem. Can anybody throw any light on this? Many thanks for any help,
NSString *audioFileName = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"caf"];
AudioFileID fileID = [self openAudioFile:audioFileName];
UInt32 filesize = [self audioFileSize:fileID];
unsigned char *outData = malloc(filesize);
OSStatus result = noErr;
result = AudioFileReadBytes(fileID, false, 0, &filesize, outData);
AudioFileClose(fileID);
if (result != 0) {
NSLog(@"Can't load file..");
}
NSUInteger bufferID;
//NSLog(@"bufferID %@", [NSNumber numberWithUnsignedInteger:bufferID]);
alGenBuffers(1, &bufferID);
//NSLog(@"bufferID %@", [NSNumber numberWithUnsignedInteger:bufferID]);
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, filesize, 44100);
[bufferStorageArray addObject:[NSNumber numberWithUnsignedInteger:bufferID]];
alGetError();
ALuint sourceID;
alGenSources(1, &sourceID);
if(alGetError() == AL_INVALID_OPERATION)
{
printf("\n++++ Error creating buffers INVALID_OPERATION!!\n");
//exit(1);
}
else
{
printf("No errors yet.");
}
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
if (loops) {
alSourcei(sourceID, AL_LOOPING, AL_TRUE);
}
[soundDictionary setObject:[NSNumber numberWithUnsignedInt:sourceID] forKey:@"sound"];
if (outData) {
free(outData);
outData = NULL;
}
[self playSound:@"sound"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您的音调问题,请确保您正在加载的声音文件与您输入 alBufferData 的采样率相匹配。您的 caf 文件可能以 22050 Hz 保存。
AudioStreamBasicDescription 的 mSampleRate 将告诉您音频文件的采样率到底是多少。
您还应该检查 mChannelsPerFrame 以确保它确实是立体声。
此外,iOS 上的 OpenAL 默认情况下仅生成 4 个立体声源。如果您尝试加载超过 4 个立体声数据源,您的音频听起来就像垃圾。您可以通过在创建上下文时指定属性 ALC_STEREO_SOURCES 和 ALC_MONO_SOURCES 来更改此设置。您最多有 32 个源(默认情况下设置 28 个单声道源和 4 个立体声源)。
For your pitch problem, make sure the sound file you are loading matches the sample rate you are feeding into alBufferData. Your caf file is probably saved at 22050 Hz.
AudioStreamBasicDescription's mSampleRate will tell you what the audio file's sample rate really is.
You should also check mChannelsPerFrame to make sure it really is stereo sound.
Also, OpenAL by default on iOS only generates 4 stereo sources. If you try to load more than 4 sources with stereo data, your audio will sound like garbage. You can change that by specifying attributes ALC_STEREO_SOURCES and ALC_MONO_SOURCES when you create a context. You have a maximum of 32 sources (by default it sets up 28 mono and 4 stereo sources).
我犯了一个愚蠢的错误 - 我在 initWithNibName 中初始化了 OpenAL,但它从未被调用过。将 init 移至 viewDidLoad 后一切正常,尽管播放速度是花栗鼠式的高速
Stupid mistake on my part - I had initialised OpenAL in initWithNibName, which was never being called. Moving the init into viewDidLoad has got everything working, although playback is chipmunk-style high speed