【问题标题】:AudioQueue Output Callback only fires 3 times (nBuffers times)AudioQueue 输出回调仅触发 3 次(nBuffers 次)
【发布时间】:2012-04-09 20:05:50
【问题描述】:

当我使用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);

【问题讨论】:

    标签: iphone audioqueue


    【解决方案1】:

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

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

    【讨论】:

    • 它能以任何方式解决我的问题吗?我说的是缓冲区用完,而不是从音频合成开始......
    • 你能提供更多信息吗?如果不是音频合成器,您要完成什么?可能是您没有正确进行设置吗?您使用的是什么设置代码?
    • 我用设置代码更新了我的问题。我只是想向我的 iPhone 扬声器输出一些东西,但我认为它在这里不相关,因为即使没有声音处理代码它也无法工作。
    • 我看了你添加的初始化代码。我看到与我的两个不同之处,不确定它们的相关性如何:(1) 在调用 AudioQueueNewOutput 时,我传递 NULL 而不是 CFRunLoopGetCurrent(),以及 (2) 在调用 AudioQueueAllocateBuffer 时,在第二个参数(大小)中我通过了你的代码中的内容:frameSize*out.mDataFormat.mBytesPerFrame(因为你称之为'frameSize'我认为是缓冲区中的帧数)
    • 我尝试在缓冲区分配期间使用NULL 而不是CFRunLoopGetCurrent()frameSize*out.mDataFormat.mBytesPerFrame,但它没有任何改变。我被卡住了:(
    【解决方案2】:

    终于发现问题出在哪里了:AudioQueue callback in simulator but not on device

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2016-04-20
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多