AudioQueue输出回调仅触发3次(nBuffers次)

时间:2012-04-09 20:05:51

标签: iphone audioqueue

当我使用AudioQueueStart(out.queue, nil)启动音频输出过程时,输出回调仅触发3次(这是已分配缓冲区的数量)。

这是我的输出回调代码:

static void AQOutputCallback(void* aqr,
                             AudioQueueRef outQ,
                             AudioQueueBufferRef outQB)
{
  AQCallbackStruct *aqc = (AQCallbackStruct *) aqr;

  NSLog(@"Out");

  // Check if AudioQueue is stopped
  if (!aqc->run) {
    NSLog(@"Stopped");
    return;
  }

  // Processing data

  // Check enqueue error
  int err = AudioQueueEnqueueBuffer(outQ, outQB, 0, NULL);
  if (err != noErr) NSLog(@"OutputCallback AudioQueueEnqueueBuffer() %d ", err);
  NSLog(@"Enqueued");
}

我认为这是由于缺少缓冲区,但我的输出是:

Out
Enqueued
Out
Enqueued
Out
Enqueued

因此,在AudioQueue开始填充第三个缓冲区之前,第一个缓冲区已入队,它不会用完缓冲区。

这里发生了什么?

编辑:设置代码

#define AUDIO_BUFFERS 3

typedef struct AQCallbackStruct {
  AudioStreamBasicDescription mDataFormat;
  AudioQueueRef queue;
  AudioQueueBufferRef mBuffers[AUDIO_BUFFERS];
  unsigned long frameSize;
  BOOL *run;
} AQCallbackStruct;

// In some method
AQCallbackStruct out;

out.mDataFormat
out.mDataFormat.mFormatID = kAudioFormatLinearPCM;
out.mDataFormat.mSampleRate = 44100.0;
out.mDataFormat.mChannelsPerFrame = 2;
out.mDataFormat.mBitsPerChannel = 16;
out.mDataFormat.mBytesPerPacket =
  out.mDataFormat.mBytesPerFrame =
    out.mDataFormat.mChannelsPerFrame * sizeof(short int);
out.mDataFormat.mFramesPerPacket = 1;
out.mDataFormat.mFormatFlags =
    kLinearPCMFormatFlagIsBigEndian
  | kLinearPCMFormatFlagIsSignedInteger
  | kLinearPCMFormatFlagIsPacked;

out.frameSize = 735;

int err;
err = AudioQueueNewOutput(&out.mDataFormat,
                          AQOutputCallback,
                          &out,
                          CFRunLoopGetCurrent(),
                          kCFRunLoopCommonModes,
                          0,
                          &out.queue);

if (err != noErr) NSLog(@"AudioQueueNewOutput() error: %d", err);


for (int i=0; i<AUDIO_BUFFERS; i++) {         
  err = AudioQueueAllocateBuffer(out.queue, out.frameSize, &out.mBuffers[i]);
  if (err != noErr) NSLog(@"Output AudioQueueAllocateBuffer() error: %d", err);
  out.mBuffers[i]->mAudioDataByteSize = out.frameSize;

  err = AudioQueueEnqueueBuffer(out.queue, out.mBuffers[i], 0, NULL);
  if (err != noErr) NSLog(@"Output AudioQueueEnqueueBuffer() error: %d", err);
}

AudioQueueStart(out.queue, nil);

2 个答案:

答案 0 :(得分:0)

请查看此页:where to start with audio synthesis on iPhone

那里的BleepMachine示例代码是我开始使用它的原因。

答案 1 :(得分:0)

我终于找到问题所在:AudioQueue callback in simulator but not on device

但是,输出回调在模拟器中正确触发但在我的设备上没有,我仍然不知道为什么AudioSession设置存在差异。