【问题标题】:AudioQueue doesn't produce any sound. What the problem can be? [duplicate]AudioQueue 不会产生任何声音。可能是什么问题? [复制]
【发布时间】:2020-12-07 16:21:18
【问题描述】:

我编写了一个用于测试 AudioToolbox Queue 的小型 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 被连续调用,我检查了。但是我听不到任何声音。可能是什么问题?

【问题讨论】:

标签: c++ macos core-audio audiotoolbox


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多