Android emulator OpenSL ES无法在Android emulator上播放音频

Android emulator OpenSL ES无法在Android emulator上播放音频,android-emulator,android-ndk,pcm,opensl,Android Emulator,Android Ndk,Pcm,Opensl,我将amrnb解码到PCM,然后将正确的PCM缓冲区放入队列缓冲区(我确信PCM数据是正确的),但没有听到声音。输入缓冲区时,日志输出: /AudioTrack(14857): obtainBuffer timed out (is the CPU pegged?) 我的代码如下,我的问题是: 我使用OpenSL ES时有什么问题吗 OpenSL ES是否只在真实设备上工作 示例代码: void AudioTest() { StartAudioPlay(); whil

我将amrnb解码到PCM,然后将正确的PCM缓冲区放入队列缓冲区(我确信PCM数据是正确的),但没有听到声音。输入缓冲区时,日志输出:

/AudioTrack(14857): obtainBuffer timed out (is the CPU pegged?)
我的代码如下,我的问题是:

  • 我使用OpenSL ES时有什么问题吗
  • 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 );
    }
    

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

    在我的应用程序中,我还必须支持Android2.2,因此我有一个后备方法来使用JNI访问Java AudioTrack API。我在我的应用程序中添加了一个特例,在检测到仿真器时始终使用AudioTrack接口