ios - AudioQueue回调仅在iOS 7上获得空缓冲区

标签 ios ios7 audioqueue audioformat

我有一个奇怪的问题。我的代码在iOS 5和6上都可以正常运行,但是在iOS 7上运行时,我在AudioQueue回调中获得了空缓冲区。

可能的相关代码:

- (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; 

}

回调:
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...

}

顺便说一句,麦克风许可正常(启用对麦克风的访问)。

更新:
似乎AudioQueueStart失败,错误为-50。这仅在iOS 7上发生。我设置的参数有问题吗?

最佳答案

我发现了问题!似乎在iOS 7上也需要设置此设置(我认为这实际上只是一个实例,因此很难找到,并且不在任何地方编写)。只需在调用任何AudioQueue函数之前添加以下代码即可:

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

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

AudioSessionSetActive(true);

希望对别人有帮助。

可以帮助您找到另一个资源here

关于ios - AudioQueue回调仅在iOS 7上获得空缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18959554/

相关文章:

ios - 创建后更改 UITableViewCell 高度

iphone - ios AudioQueue 流式传输多首歌曲

ios - swift 2 错误 : Editor placeholder in sourcefile

ios - 当我将框拖放到屏幕的某些区域时,如何才能将它们固定到位?

ios - ios 7/XCode 5 中对 sqlite 的更改

objective-c - iOS 7 sizeWithAttributes : replacement for sizeWithFont:constrainedToSize

ios - 有没有办法复制EKEvent,或者让它符合NSCopying协议(protocol)

ios - 在 Swift 3 中使用音频队列获取麦克风输入

objective-c - iPhone 上的混音 : Need advice

iOS 联系人框架 : How to fetch contacts by phone number