Android OpenSL"pAudioSrc:数据格式2不允许" - 拒绝SL_DATAFORMAT_PCM?

mxd*_*ois 2 java android android-ndk opensl

我正在尝试使用bufferqueue source和outputmix sink创建一个AudioPlayer.我已经使用与ndk示例中显示的pcm格式非常相似的pcm格式配置了源,但OpenSL拒绝AudioPlayer("数据格式2").这对我没有任何意义.

这是错误(在三星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)
Run Code Online (Sandbox Code Playgroud)

这是相关的代码:

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);
Run Code Online (Sandbox Code Playgroud)

任何人都知道是什么导致了这个?谢谢!

Rea*_*hed 5

可能是您的设备不支持音频采样率.尝试SL_SAMPLINGRATE_8 代替SL_SAMPLINGRATE_48或选择其他设备(Nexus 4/HTC One)进行测试.

如果在语音通信时听到失真的声音,则增加录音机缓冲区大小,您也可以尝试更改采样率.其他采样速率选项有:SL_SAMPLINGRATE_16,SL_SAMPLINGRATE_32,SL_SAMPLINGRATE_44_1等.

每个Android设备都有一个特定/首选的缓冲区大小和采样率.您可以从以下java代码段获得首选缓冲区大小和采样率.请注意,audioManager.getProperty()不适用于API级别<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);
Run Code Online (Sandbox Code Playgroud)