Audio Queue : AudioQueueStart returns -50

2019-05-07 03:32发布

问题:

I'm trying to write a microphone power meter module in a GLES app (Unity3d). It works fine in UIKit application. But when I integrate into my unity3d project, the AudioQueue cannot start property.The result code of calling AudioQueueStart is always -50, but what does -50 mean? I can't find a reference in iOS Developer Library.

I have searched about this problem and know someone has the same problem in a cocos2d application. Maybe this has some relevance.

Here is my code for Start Audio Queue:

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

回答1:

below works for me:

Add this:

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");
    }

before AudioQueueStart(myQueue, NULL);



回答2:

Have you checked that AudioSession is in AVAudioSessionCategoryRecord or AVAudioSessionCategoryPlayAndRecord mode?

http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1