我设法创建了一个OpenSL上下文,以便它播放声音
但我仍然遇到问题:我将播放机的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
比我应该播放的为什么我需要混合以填充缓冲区的样本超过44.100Hz的因子~2
?
我是否需要使用循环缓冲区(我在这里使用的仍然是OpenSL使用的?)?
提前感谢您的每一条建议和帮助!
答案 0 :(得分:0)
所以我解决了这个问题 我实际上初始化了OpenSL两次,因此也开始从一个源写入两个并发缓冲区 - 这导致一半帧没有正确播放。