我正试图在GLES应用程序(Unity3d)中编写一个麦克风功率计模块。它在UIKit应用程序中运行良好。但是当我集成到我的unity3d项目中时,AudioQueue无法启动属性。调用AudioQueueStart的结果代码总是-50,但-50是什么意思?我在iOS开发者库中找不到参考资料。
我搜索过这个问题,知道有人在cocos2d应用程序中也有同样的问题。也许这有一定的相关性。
这是我的启动音频队列代码:
UInt32 ioDataSize = sizeof(sampleRate);
AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSampleRate, &ioDataSize, &sampleRate); //returns noErr
format.mSampleRate = sampleRate;
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
format.mFramesPerPacket = format.mChannelsPerFrame = 1;
format.mBitsPerChannel = 16;
format.mBytesPerPacket = format.mBytesPerFrame = 2;
AudioQueueNewInput(&format, listeningCallback, self, NULL, NULL, 0, &queue); //returns noErr
AudioQueueBufferRef buffers[3];
for (NSInteger i = 0; i < 3; ++i) {
AudioQueueAllocateBuffer(queue, 735, &buffers[i]); //returns noErr
AudioQueueEnqueueBuffer(queue, buffers[i], 0, NULL); //returns noErr
}
levels = (AudioQueueLevelMeterState *)calloc(sizeof(AudioQueueLevelMeterState), format.mChannelsPerFrame);
UInt32 trueValue = true;
AudioQueueSetProperty(queue, kAudioQueueProperty_EnableLevelMetering, &trueValue, sizeof(UInt32)); //returns noErr
AudioQueueStart(queue, NULL); //returns -50
以下对我有效:
添加此:
AVAudioSession * session = [AVAudioSession sharedInstance];
if (!session) printf("ERROR INITIALIZING AUDIO SESSION! n");
else{
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&nsError];
if (nsError) printf("couldn't set audio category!");
[session setActive:YES error:&nsError];
if (nsError) printf("AudioSession setActive = YES failed");
}
AudioQueueStart(myQueue, NULL);
之前
您是否已检查AudioSession是否处于AVAudioSessionCategoryRecord或AVAudioSsessionCategoryPlayAndRecord模式?
http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1