无法使用 AndroidSimpleBufferQueue 播放 pcm > 60kb

Cannot play pcm > 60kb with the AndroidSimpleBufferQueue

我已经初始化了一个 AndroidSimpleBufferQueue,例如值:

SLDataLocator_AndroidSimpleBufferQueue bufferLocator = {
  SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
SLDataFormat_PCM pcmFormat = {
  SL_DATAFORMAT_PCM,           1,
  SL_SAMPLINGRATE_44_1,        SL_PCMSAMPLEFORMAT_FIXED_16,
  SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_CENTER,
  SL_BYTEORDER_LITTLEENDIAN};

SLDataSource            audioSource      = {&bufferLocator, &pcmFormat};
SLDataLocator_OutputMix locatorOutputMix = {SL_DATALOCATOR_OUTPUTMIX,
                                            outputMixObj.getObject()};
SLDataSink audioSink = {&locatorOutputMix, nullptr};
[...]

然后当我需要播放声音时,我会排队,例如:

bufferQueue->Enqueue(bufferQueue, (uint8_t*)buffer.data(),
          buffer.size())

它适用于小于 60k 的缓冲区,但是当缓冲区更大时,没有声音播放或有时只是一个伪影。

来自规格:

The buffers that are queued are used in place and are not required to be copied by the device, although this may be implementation-dependent. The application developer should be aware that modifying the content of a buffer after it has been queued is undefined and can cause audio corruption

因此内存必须保持有效直到声音完全播放。

阅读文档并注意意外的结构复制:)