AudioQueue 不产生任何声音。可能是什么问题?

AudioQueue doesn't produce any sound. What the problem can be?

我编写了一个用于测试 AudioToolbox 队列的小型 C++ 应用程序。我正在用一些随机数据填充缓冲区只是为了检查它是否有效。

static void HandleOutputBuffer(void* inUserData, AudioQueueRef queue, AudioQueueBufferRef inBuffer) {
    memset(inBuffer->mAudioData, rand() % 256, inBuffer->mAudioDataByteSize);
    throwExceptionIfError(AudioQueueEnqueueBuffer(queue, inBuffer, 0, NULL));
}

int main(int argc, const char * argv[]) {
    AudioQueueRef queue;
    AudioStreamBasicDescription format = {0};
    format.mBytesPerFrame = 4;
    format.mBitsPerChannel = 16;
    format.mChannelsPerFrame = 2;
    format.mFormatID = kAudioFormatLinearPCM;
    format.mFramesPerPacket = 1;
    format.mBytesPerPacket = format.mBytesPerFrame;
    format.mSampleRate = 44100;
    format.mReserved = 0;
    format.mFormatFlags =  kAudioFormatFlagIsSignedInteger |
            kAudioFormatFlagsNativeEndian |
            kLinearPCMFormatFlagIsPacked;
    throwExceptionIfError(AudioQueueNewOutput(&format, HandleOutputBuffer, NULL,
            NULL, // Use internal thread
            kCFRunLoopDefaultMode,
            0, // Reserved, must be 0
            &queue));

    AudioQueueBufferRef buffer;
    throwExceptionIfError(AudioQueueAllocateBuffer(queue, 1024, &buffer));
    buffer->mAudioDataByteSize = 1024;
    throwExceptionIfError(AudioQueueEnqueueBuffer(queue, buffer, 0, NULL));
    throwExceptionIfError(AudioQueueStart(queue, NULL));

    @autoreleasepool {
        // Setup code that might create autoreleased objects goes here.
    }
    return NSApplicationMain(argc, argv);
}

HandleOutputBuffer 被连续调用,我查过了。但是我听不到任何声音。可能是什么问题?

您的代码唯一的问题是它没有将足够的缓冲区排队。 AudioQueues 似乎喜欢至少有两个:

AudioQueueBufferRef buffer;
for (int i = 0; i < 2; i++) {
    throwExceptionIfError(AudioQueueAllocateBuffer(queue, 1024, &buffer));
    buffer->mAudioDataByteSize = 1024;
    throwExceptionIfError(AudioQueueEnqueueBuffer(queue, buffer, 0, NULL));
}