添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
曾经爱过的汉堡包  ·  PHP ...·  5 月前    · 
坐怀不乱的丝瓜  ·  C++ ...·  1 年前    · 
心软的刺猬  ·  通用Mapper ...·  1 年前    · 
文武双全的罐头  ·  [MarkDown] ...·  1 年前    · 
音视频开发技术(23) OpenSL 录制及播放 PCM 音频数据

音视频开发技术(23) OpenSL 录制及播放 PCM 音频数据

目录

  • Android OpenSL 录制 PCM 音频数据
  • Android OpenSL ES播放PCM 数据

音视频开发知识点路线图:

一、Android OpenSL 录制 PCM 音频数据

开发准备

1.在Android studo下面建立工程,在使用OpenSL ES的API之前,首先需要引入CMakeLists.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++文件

add_library( # Sets the name of the library.
             native-lib
             # Sets the library as a shared library.
             SHARED
             native-lib.cpp
            )

OpenSL ES 录制 PCM 音频数据流程主要有如下:

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

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

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

//引擎接口
static SLObjectItf engineObject = NULL;
//引擎对象
static SLEngineItf engineEngine;
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、设置IO设备麦克风,设置buffer队列,设置录制规格。

 /**
     * 设置IO设备(麦克风)
    SLDataLocator_IODevice loc_dev = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
                                      SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
    SLDataSource audioSrc = {&loc_dev, NULL};
     * 设置buffer队列
    SLDataLocator_AndroidSimpleBufferQueue loc_bq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
     * 设置录制规格:PCM、2声道、44100HZ、16bit
    SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 2, SL_SAMPLINGRATE_44_1,
                                   SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
                                   SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, SL_BYTEORDER_LITTLEENDIAN};
    SLDataSink audioSnk = {&loc_bq, &format_pcm};
    const SLInterfaceID id[1] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
    const SLboolean req[1] = {SL_BOOLEAN_TRUE};

3、创建录音器

 /**
     * 创建录制器
    result = (*engineEngine)->CreateAudioRecorder(engineEngine, &recorderObject, &audioSrc,
                                                  &audioSnk, 1, id, req);
    if (SL_RESULT_SUCCESS != result) {
        return;
    result = (*recorderObject)->Realize(recorderObject, SL_BOOLEAN_FALSE);
    if (SL_RESULT_SUCCESS != result) {
        return;
    result = (*recorderObject)->GetInterface(recorderObject, SL_IID_RECORD, &recorderRecord);
    result = (*recorderObject)->GetInterface(recorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
                                             &recorderBufferQueue);
    finished = false;
    result = (*recorderBufferQueue)->Enqueue(recorderBufferQueue, recordBuffer->getRecordBuffer(),
                                             recorderSize);
    result = (*recorderBufferQueue)->RegisterCallback(recorderBufferQueue, bqRecorderCallback, NULL);

4、设置回调函数

  result = (*recorderBufferQueue)->RegisterCallback(recorderBufferQueue, bqRecorderCallback, NULL);
void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
    // for streaming recording, here we would call Enqueue to give recorder the next buffer to fill
    // but instead, this is a one-time buffer so we stop recording
    fwrite(recordBuffer->getNowBuffer(), 1, recorderSize, inputPcmFile);
    if(finished)
        (*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_STOPPED);
        fclose(inputPcmFile);
    } else{
        (*recorderBufferQueue)->Enqueue(recorderBufferQueue, recordBuffer->getRecordBuffer(),
                                        recorderSize);
}

5、开始录音

 /**
     * 开始录音
    (*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_RECORDING);

通过上面五个步骤就完成了Android OpenGL ES录制pcm功能。

相关视频推荐:

LinuxC++音视频开发视频 免费】FFmpeg/WebRTC/RTMP/NDK/Android音视频流媒体高级开发

【文章福利】:音视频面试题、学习资料、教学视频和学习路线图资料 (资料包括C/C++,Linux,FFmpeg webRTC rtmp hls rtsp ffplay srs 等), qun994289133 免费分享 ,有需要的可以加群领取哦! ~学习交流裙994289133加入领取资料

企鹅群994289133领取资料

二、Android OpenSL ES播放PCM 数据

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

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

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

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

// 引擎接口
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.创建混音器

//第二步,创建混音器
    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格式的频率位数等信息并建立播放器

// 第三步,配置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.设置缓冲队列,音量设置和回调函数

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

缓存队列回调函数

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