Android OpenSL ES播放PCM 数据(九)

2021-01-11 09:51:45 浏览数 (1)

前言

本章主要讲述如何通过Android OpenSL 实现播放 PCM 音频数据。开发流程回顾可以看下笔者前面发布的文章

开发准备

1.在Android studo下面建立工程,在使用OpenSL ES的API之前,首先需要引入CMakeLists.txt里面添加依赖库和链接库,代码如下:

代码语言:txt复制
find_library( # Sets the name of the path variable.
              log-lib
              android
        # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib
                       OpenSLES
                       android
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

2.同时添加静态库依赖的c 文件

代码语言:txt复制
add_library( # Sets the name of the library.
             native-lib
             # Sets the library as a shared library.
             SHARED
             native-lib.cpp
            )

OpenSL ES 的开发流程主要有如下:

1、 新建native-lib.cpp文件,然后引入头文件

代码语言:txt复制
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

和录制pcm数据一样,声明SLObjectItf接口类型的引擎接口对象engineObject,而后用方法slCreateEngine建立一个引擎接口对象;建立好引擎接口对象后,须要用SLObjectItf的Realize方法来实现engineObject;最后用SLObjectItf的GetInterface方法来初始化SLEngnineItf对象实例。如:

代码语言:txt复制
// 引擎接口
SLObjectItf engineObject = NULL;
//引擎对象
SLEngineItf engineEngine = NULL;
void createEngine()
{
    SLresult result;
    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
}

2.创建混音器

代码语言:txt复制
//第二步,创建混音器
    const SLInterfaceID mids[1] = {SL_IID_ENVIRONMENTALREVERB};
    const SLboolean mreq[1] = {SL_BOOLEAN_FALSE};
    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, mids, mreq);
    (void)result;
    result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
    (void)result;
    result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB, &outputMixEnvironmentalReverb);
    if (SL_RESULT_SUCCESS == result) {
        result = (*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties(
                outputMixEnvironmentalReverb, &reverbSettings);
        (void)result;
    }
    SLDataLocator_OutputMix outputMix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
    SLDataSink audioSnk = {&outputMix, NULL};

3.设置pcm格式的频率位数等信息并建立播放器

代码语言:txt复制
// 第三步,配置PCM格式信息
    SLDataLocator_AndroidSimpleBufferQueue android_queue={SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,2};
    SLDataFormat_PCM pcm={
            SL_DATAFORMAT_PCM,//播放pcm格式的数据
            2,//2个声道(立体声)
            SL_SAMPLINGRATE_44_1,//44100hz的频率
            SL_PCMSAMPLEFORMAT_FIXED_16,//位数 16位
            SL_PCMSAMPLEFORMAT_FIXED_16,//和位数一致就行
            SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT,//立体声(前左前右)
            SL_BYTEORDER_LITTLEENDIAN//结束标志
    };
    SLDataSource slDataSource = {&android_queue, &pcm};


    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 = (*engineEngine)->CreateAudioPlayer(engineEngine, &pcmPlayerObject, &slDataSource, &audioSnk, 3, ids, req);
    //初始化播放器
    (*pcmPlayerObject)->Realize(pcmPlayerObject, SL_BOOLEAN_FALSE);

//    得到接口后调用  获取Player接口
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_PLAY, &pcmPlayerPlay);

4.设置缓冲队列,音量设置和回调函数

代码语言:txt复制
//    注册回调缓冲区 获取缓冲队列接口
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_BUFFERQUEUE, &pcmBufferQueue);
    //缓冲接口回调
    (*pcmBufferQueue)->RegisterCallback(pcmBufferQueue, pcmBufferCallBack, NULL);
    //    获取音量接口
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_VOLUME, &pcmPlayerVolume);

缓存队列回调函数

代码语言:txt复制
void pcmBufferCallBack(SLAndroidSimpleBufferQueueItf bf, void * context)
{
    //assert(NULL == context);
    getPcmData(&buffer);
    // for streaming playback, replace this test by logic to find and fill the next buffer
    if (NULL != buffer) {
        SLresult result;
        // enqueue another buffer
        result = (*pcmBufferQueue)->Enqueue(pcmBufferQueue, buffer, 44100 * 2 * 2);
        // the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT,
        // which for this code example would indicate a programming error
    }
}

5.设置播放状态并手动开始调用回调函数

代码语言:txt复制
//    获取播放状态接口
    (*pcmPlayerPlay)->SetPlayState(pcmPlayerPlay, SL_PLAYSTATE_PLAYING);

//    主动调用回调函数开始工作
    pcmBufferCallBack(pcmBufferQueue, NULL);

小结

通过上面五个步骤就完成了Android OpenGL ES播放pcm功能,文章最后会附上demo链接,demo播放的pcm文件路径为

/Android/data/com.pengjie0668.opensles.demo/cache/temp.pcm

github Demo下载链接

0 人点赞