AudioQueue erzeugt keinen Ton. Was kann das Problem sein? [Duplikat]
Dec 08 2020
Ich habe eine kleine C ++ - App zum Testen der AudioToolbox-Warteschlange geschrieben. Ich fülle den Puffer mit zufälligen Daten, um zu überprüfen, ob es funktioniert.
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 wird kontinuierlich aufgerufen, habe ich überprüft. Ich kann jedoch keinen Ton hören. Was kann das Problem sein?
Antworten
1 RhythmicFistman Dec 08 2020 at 05:03
Das einzige Problem, das Ihr Code hat, ist, dass er nicht genügend Puffer in die Warteschlange stellt. AudioQueues
scheinen es zu mögen, mindestens zwei zu haben:
AudioQueueBufferRef buffer;
for (int i = 0; i < 2; i++) {
throwExceptionIfError(AudioQueueAllocateBuffer(queue, 1024, &buffer));
buffer->mAudioDataByteSize = 1024;
throwExceptionIfError(AudioQueueEnqueueBuffer(queue, buffer, 0, NULL));
}