Question

I'm trying to create an AudioPlayer with a bufferqueue source and outputmix sink. I've configured the source with a pcm format very similar to that shown in the ndk samples, but OpenSL is rejecting SL_DATAFORMAT_PCM ("data format 2"). This doesn't make any sense to me.

Here's the error (on a Samsung Galaxy S2):

02-27 15:43:47.315: E/libOpenSLES(12681): pAudioSrc: data format 2 not allowed
02-27 15:43:47.315: W/libOpenSLES(12681): Leaving Engine::CreateAudioPlayer (SL_RESULT_CONTENT_UNSUPPORTED)

and here's the relevant code:

SLuint32 channels = 2;
SLuint32 speakers = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
SLuint32 sr = SL_SAMPLINGRATE_48;

//...
SLDataFormat_PCM format_pcm = {
    SL_DATAFORMAT_PCM,
    channels,
    sr,
    SL_PCMSAMPLEFORMAT_FIXED_16,
    SL_PCMSAMPLEFORMAT_FIXED_16,
    speakers,
    SL_BYTEORDER_LITTLEENDIAN
};

// Configure audio player source
SLDataLocator_AndroidBufferQueue loc_bufq =
    {SL_DATALOCATOR_ANDROIDBUFFERQUEUE, 2};
SLDataSource audioSrc = {&loc_bufq, &format_pcm};

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

// create audio player
const SLInterfaceID iidsOutPlayer[] = {SL_IID_ANDROIDBUFFERQUEUESOURCE};
const SLboolean reqsOutPlayer[] = {SL_BOOLEAN_TRUE};
result = (*engineItf)->CreateAudioPlayer(
                        engineItf,
                        &(outPlayerObject),
                        &audioSrc, &audioSnk,
                        1, iidsOutPlayer,reqsOutPlayer);

Does anyone know what's causing this? Thanks!

Was it helpful?

Solution 2

It turns out I needed to be using the SLDataLocator_AndroidSimpleBufferQueue instead of SLDataLocator_AndroidBufferQueue.

OTHER TIPS

May be the audio sampling rate is not supported in your device. Try SL_SAMPLINGRATE_8 instead of SL_SAMPLINGRATE_48 or pick some other device (Nexus 4/HTC One) to test.

If you hear distorted sound while voice communication then increase your recorder buffer size and you may try changing sampling rate too. Other sampling rate options are: SL_SAMPLINGRATE_16, SL_SAMPLINGRATE_32, SL_SAMPLINGRATE_44_1 etc.

There's a specific/preferred buffer size and sampling rate for each android device. You can have the preferred buffer size and sampling rate from following java code segment. Note that, audioManager.getProperty() doesn't work with API level<17.

AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
String rate = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
String size = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
Log.d("Buffer Size and sample rate", "Size :" + size + " & Rate: " + rate);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top