当我使用 启动音频输出过程时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);