2

我正在尝试AudioPlayer使用缓冲队列源和输出混合接收器创建一个。我已将源配置为与 ndk 示例中显示的非常相似的 pcm 格式,但 OpenSL 拒绝SL_DATAFORMAT_PCM(“数据格式 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)

这是相关的代码:

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);

有谁知道这是什么原因造成的?谢谢!

4

2 回答 2

5

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

如果您在语音通信时听到失真的声音,请增加录音机缓冲区大小,您也可以尝试更改采样率。其他采样率选项有:SL_SAMPLINGRATE_16SL_SAMPLINGRATE_32SL_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);
于 2014-03-13T10:43:46.060 回答
1

事实证明我需要使用SLDataLocator_AndroidSimpleBufferQueue而不是SLDataLocator_AndroidBufferQueue.

于 2014-04-08T03:53:23.117 回答