我设法创建了一个OpenSL上下文以及所有这些,以便它可以播放声音。
但是我仍然有一个问题:我将Player的samplesPerSec设置为44.100 Hz,但实际上它是在〜70.000 Hz上运行的。如何解决这个问题?

好吧,我会告诉你我做了什么(在Source Code上完整介绍):

PolyOpenSLInterface.cpp中,我正在初始化OpenSL并开始将缓冲区放入SLAndroidSimpleBufferQueueItf中。
我通过以下方式创建播放器:

SLDataLocator_AndroidSimpleBufferQueue lDataLocatorIn;
lDataLocatorIn.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
lDataLocatorIn.numBuffers = 1;

SLDataFormat_PCM lDataFormat;
lDataFormat.formatType = SL_DATAFORMAT_PCM;
lDataFormat.numChannels = POLY_NUM_CHANNELS;
lDataFormat.samplesPerSec = SL_SAMPLINGRATE_44_1; //sampling rate 44.100Hz
lDataFormat.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
lDataFormat.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
if(POLY_NUM_CHANNELS > 1){
    lDataFormat.channelMask = SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_LEFT;
} else {
    lDataFormat.channelMask = SL_SPEAKER_FRONT_CENTER;
}
lDataFormat.endianness = SL_BYTEORDER_LITTLEENDIAN;

SLDataSource lDataSource;
lDataSource.pLocator = &lDataLocatorIn;
lDataSource.pFormat = &lDataFormat;

SLDataLocator_OutputMix lDataLocatorOut;
lDataLocatorOut.locatorType = SL_DATALOCATOR_OUTPUTMIX;
lDataLocatorOut.outputMix = mOutputMixObj;

SLDataSink lDataSink;
lDataSink.pLocator = &lDataLocatorOut;
lDataSink.pFormat = NULL;

const SLuint32 lSoundPlayerIIDCount = 2;
const SLInterfaceID lSoundPlayerIIDs[] = { SL_IID_PLAY, SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
const SLboolean lSoundPlayerReqs[] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };

lRes = (*mEngine)->CreateAudioPlayer(mEngine, &mPlayerObj, &lDataSource, &lDataSink, lSoundPlayerIIDCount, lSoundPlayerIIDs, lSoundPlayerReqs);
lRes = (*mPlayerObj)->Realize(mPlayerObj, SL_BOOLEAN_FALSE);

我注册的回调看起来像这样(我将已经用过的缓冲区放入下一个示例,然后将其重新放入队列):
void OpenSLAudioInterface::queueCallback(SLAndroidSimpleBufferQueueItf caller, void* pContext){
    OpenSLAudioInterface *audioInterface = (OpenSLAudioInterface*) pContext;
    if(audioInterface->buffer && audioInterface->getMixer()) {
        int16_t *out = (int16_t*)audioInterface->buffer;
        audioInterface->getMixer()->mixIntoBuffer(out, POLY_FRAMES_PER_BUFFER);
        (*(audioInterface->mPlayerQueue))->Enqueue(audioInterface->mPlayerQueue, out, sizeof(int16_t)*POLY_FRAMES_PER_BUFFER*POLY_NUM_CHANNELS);
    }
}

谁能解释,为什么播放的声音比~2 times faster还要大,为什么我需要混合以填充缓冲区的样本超过~2的44.100Hz?
我是否需要使用循环缓冲区(OpenSL是否仍在使用我在这里使用的缓冲区)?

感谢您提出的每一个建议和帮助!

最佳答案

所以我解决了这个问题。
我实际上对OpenSL进行了两次初始化,因此也开始从一个源写入两个并发的缓冲区队列-这导致一半的帧无法正确播放。

10-08 12:33