【问题标题】:AudioQueue callback get empty buffer on iOS 7 onlyAudioQueue 回调仅在 iOS 7 上获取空缓冲区
【发布时间】:2013-09-28 08:48:53
【问题描述】:

我遇到了一个奇怪的问题。我的代码在 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 上发生。我设置的参数有问题吗?

【问题讨论】:

  • 你有没有找到关于 -50 错误问题的解决方案?
  • 只是想说一句非常感谢你,感谢你的代码示例和你在答案中链接的书,我能够做出我需要的东西:)
  • 如果有人试图让你的代码工作并最终在 ios 7 上崩溃,请注意:试试这个:audioFormat.mBytesPerFrame = (audioFormat.mBitsPerChannel/8) * audioFormat.mChannelsPerFrame ; audioFormat.mBytesPerPacket = (audioFormat.mBitsPerChannel/8) * audioFormat.mChannelsPerFrame;
  • 对于大于7的iOS版本。尝试this

标签: ios ios7 audioqueue audioformat


【解决方案1】:

我发现了问题!似乎在 iOS 7 上也需要设置它(我认为这只是实际上,因此很难找到,没有写在任何地方)。只需在调用任何 AudioQueue 函数之前添加此代码:

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

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

AudioSessionSetActive(true);

希望对其他人有所帮助。

可以找到另一个可以提供帮助的资源here

【讨论】:

  • 我也有类似的问题并通过添加上面的代码行来解决,但这些方法在 iOS 7 中已被弃用。
  • @MuhammadZeeshan 这是一个旧答案,但仍然有效。你找到新的解决方案了吗? (我很乐意更新这个)
【解决方案2】:

@Idan 你的回答是正确的并且有效,但它只在应用程序最小部署目标是 iOS 7 时显示警告。对于 iOS 7,我们可以这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多