Question

I got a strange issue. My code works great on both iOS 5&6 but when running on iOS 7 I get empty buffers on the AudioQueue callback.

Possible relevant code:

- (void)setUpAudioFormat
{
audioFormat.mFormatID         = kAudioFormatLinearPCM;
audioFormat.mSampleRate       = SAMPLE_RATE;//16000.0;
audioFormat.mChannelsPerFrame = CHANNELS;//1;
audioFormat.mBitsPerChannel   = 16;
audioFormat.mFramesPerPacket  = 1;
audioFormat.mBytesPerFrame    = audioFormat.mChannelsPerFrame * sizeof(SInt16);
audioFormat.mBytesPerPacket   = audioFormat.mBytesPerFrame * audioFormat.mFramesPerPacket;
audioFormat.mFormatFlags      = kLinearPCMFormatFlagIsSignedInteger
| kLinearPCMFormatFlagIsPacked;

bufferNumPackets = 2048;  // must be power of 2 for FFT!
bufferByteSize = [self byteSizeForNumPackets:bufferNumPackets];

}

- (UInt32)numPacketsForTime:(Float64)seconds
{
return (UInt32) (seconds * audioFormat.mSampleRate / audioFormat.mFramesPerPacket);
}

- (UInt32)byteSizeForNumPackets:(UInt32)numPackets
{
return numPackets * audioFormat.mBytesPerPacket;
}

- (void)setUpRecordQueue
{
NSLog(@"\n+++ setUpRecordQueue");
OSStatus errorStatus = AudioQueueNewInput(
                   &audioFormat,
                   recordCallback,
                   self,                // userData
                   CFRunLoopGetMain(),  // run loop
                   NULL,                // run loop mode
                   0,                   // flags
                   &recordQueue);

if (errorStatus) {
    NSLog(@"\n\n ERROR : Error %ld on AudioQueueNewInput\n", errorStatus );
}


if (recordQueue == nil) {
    NSLog(@"\n\n ----- Record Queue is nil! -----");
}

UInt32 trueValue = true;
       AudioQueueSetProperty(recordQueue,kAudioQueueProperty_EnableLevelMetering,&trueValue,sizeof (UInt32));
}

- (void)setUpRecordQueueBuffers
{
NSLog(@"\n+++ setUpRecordQueueBuffers");
assert(recordQueue != nil);
for (int t = 0; t < NUMBER_AUDIO_DATA_BUFFERS; ++t)
{
    OSStatus errorStatus = AudioQueueAllocateBuffer(
                             recordQueue,
                             bufferByteSize,
                             &recordQueueBuffers[t]);
    if (errorStatus) {
        NSLog(@"\n\n ERROR : Error %ld on AudioQueueAllocateBuffer\n", errorStatus );
    }
}
}

- (void)primeRecordQueueBuffers
{
NSLog(@"\n+++ primeRecordQueueBuffers");
assert(recordQueue != nil);
for (int t = 0; t < NUMBER_AUDIO_DATA_BUFFERS; ++t)
{
    OSStatus errorStatus = AudioQueueEnqueueBuffer(
                            recordQueue,
                            recordQueueBuffers[t],
                            0,
                            NULL);
    if (errorStatus) {
        NSLog(@"\n\n ERROR : Error %ld on AudioQueueEnqueueBuffer\n", errorStatus );
    }
}
}

- (void)startRecording
{
[self startRecording:FALSE];
}

- (void)startRecording:(BOOL) autoStop
{
NSLog(@"Starting to record");

recording = YES;
shouldStopRecording = NO;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
               , ^{
    NSLog(@"PPPP C1");
    _frameIndex= 0;
    self.fileWasCreated = NO;
    [self setUpRecordQueue];
    NSLog(@"PPPP C2");
    [self setUpRecordQueueBuffers];
    NSLog(@"PPPP C3");
    [self primeRecordQueueBuffers];
    NSLog(@"PPPP C4");

    AudioQueueStart(recordQueue, NULL);
    NSLog(@"PPPP C5");

    if (autoStop) {
        [self stopRecording];
    }

});

}

- (void)stopRecording
{
NSLog(@"Stoping to record");
if (recordQueue != nil) {
    NSString *osVersion = [[UIDevice currentDevice]  systemVersion];

    if ([osVersion doubleValue]<6){
        AudioQueueDispose(recordQueue, TRUE);
    }
    else {
        AudioQueueStop(recordQueue, FALSE);
    }

    recordQueue = nil;
}

NSLog(@"Stopped recording");

shouldStopRecording = YES;
recording = NO; 

}

The Callback:

static void recordCallback(
                       void* inUserData,
                       AudioQueueRef inAudioQueue,
                       AudioQueueBufferRef inBuffer,
                       const AudioTimeStamp* inStartTime,
                       UInt32 inNumPackets,
                       const AudioStreamPacketDescription* inPacketDesc)
{
NSLog(@"recordCallback %u", (unsigned int)inBuffer->mAudioDataByteSize);
// I get always zero here...

}

Btw, The mic permission is ok (enable access to mic).

UPDATE: Seems like the AudioQueueStart failed with error -50. This happens on iOS 7 only. Is there any issue with parameters I've set?

Was it helpful?

Solution

I found the issue! seems like on iOS 7 there is a need to set this also (I assume this is only practically therefore it's hard to find, isn't written anywhere). Just add this code before calling any AudioQueue function:

AudioSessionInitialize(NULL,
                       NULL,
                       nil,
                       ( void *)(self)
                       );

UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                        sizeof(sessionCategory),
                        &sessionCategory
                        );

AudioSessionSetActive(true);

Hope that would help others.

Another resource that can help can be found here.

OTHER TIPS

@Idan your answer is correct and working but it only shows warning if app minimun deployment target is iOS 7. For iOS 7 we can do something like this:

NSError *audioSessionError;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&audioSessionError];

if(audioSessionError)
{
    NSLog(@"AVAudioSession error setting category:%@",audioSessionError);
}
else
{
    [audioSession setActive:YES error:&audioSessionError];
    if(audioSessionError)
        NSLog(@"AVAudioSession error activating: %@",audioSessionError);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top