3

我将 amrnb 解码为 PCM,然后将正确的 pcm 缓冲区放入 Enqueue 缓冲区(我确定 PCM 数据是正确的),但听不到声音。并且在提供缓冲区时,记录输出:

/AudioTrack(14857): obtainBuffer timed out (is the CPU pegged?)

我的代码如下,我的问题是:

  1. 使用 OpenSL ES 时有什么问题吗?
  2. OpenSL ES 真的只能在真机上运行吗?

示例代码:

void AudioTest()
{    

    StartAudioPlay();

    while(1)
    {
          //decode AMR to PCM

      /* Convert to little endian and write to wav */

          //write buffer to buffer queue     
      AudioBufferWrite(littleendian, 320);

   }
}

void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{

//do nothing

}

void AudioBufferWrite(const void* buffer, int size) 
{

    (*gBQBufferQueue)->Enqueue(gBQBufferQueue, buffer, size );

}

// create buffer queue audio player
void SlesCreateBQPlayer(/*AudioCallBackSL funCallback, void *soundMix,*/ int rate, int nChannel, int bitsPerSample ) 
{

    SLresult result;

    // configure audio source
    SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};

    SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 1, SL_SAMPLINGRATE_8,
        SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
        SL_SPEAKER_FRONT_CENTER, SL_BYTEORDER_LITTLEENDIAN};

    SLDataSource audioSrc = {&loc_bufq, &format_pcm};

    // configure audio sink
    SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, gOutputMixObject};
    SLDataSink audioSnk = {&loc_outmix, NULL};

    // create audio player
    const SLInterfaceID ids[3] = {SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND, SL_IID_VOLUME};
    const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
    result = (*gEngineEngine)->CreateAudioPlayer(gEngineEngine, &gBQObject, &audioSrc, &audioSnk,
            3, ids, req);

    // realize the player
    result = (*gBQObject)->Realize(gBQObject, SL_BOOLEAN_FALSE);

    // get the play interface
    result = (*gBQObject)->GetInterface(gBQObject, SL_IID_PLAY, &gBQPlay);

    // get the buffer queue interface
    result = (*gBQObject)->GetInterface(gBQObject, SL_IID_BUFFERQUEUE,
            &gBQBufferQueue);

    // register callback on the buffer queue
    result = (*gBQBufferQueue)->RegisterCallback(gBQBufferQueue, bqPlayerCallback, NULL/*soundMix*/);

    // get the effect send interface
    result = (*gBQObject)->GetInterface(gBQObject, SL_IID_EFFECTSEND,
            &gBQEffectSend); 

    // set the player's state to playing
    result = (*gBQPlay)->SetPlayState(gBQPlay, SL_PLAYSTATE_PLAYING );
}
4

1 回答 1

0

我不完全确定,但我认为你是正确的,因为模拟器的 OpenSL ES 支持实际上不起作用。我从来没有让它在实践中工作,而它可以在我尝试过的任何设备上工作。

在我的应用程序中,我还必须支持 Android 2.2,因此我可以使用 JNI 来访问 Java AudioTrack API。我在我的应用程序中添加了一个特殊情况,以便在检测到模拟器时始终使用 AudioTrack 接口。

于 2012-09-24T07:01:05.157 回答