Android利用opensl-es实现录音

来源:互联网 发布:php短信轰炸机源码 编辑:程序博客网 时间:2024/04/30 17:45

一.创建引擎对象,申请内存空间,获取引擎接口

SL_API SLresult SLAPIENTRY slCreateEngine(
SLObjectItf             *pEngine,
SLuint32                numOptions,
const SLEngineOption    *pEngineOptions,
SLuint32                numInterfaces,
const SLInterfaceID     *pInterfaceIds,
const SLboolean         * pInterfaceRequired
);

(*pEngine)->Realize(pEngine, SL_BOOLEAN_FALSE);

(*pEngine)->GetInterface(pEngine, SL_IID_ENGINE, &engineEngine);


二.创建AudioRecorder

1.获取audio source

   SLDataLocator_IODevice loc_dev = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
            SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
    SLDataSource audioSrc = {&loc_dev, NULL};

2.配置audio sink

    SLDataLocator_AndroidSimpleBufferQueue loc_bq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, NB_BUFFERS_IN_QUEUE};
    SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 1, simplingRate,
        SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
              SL_SPEAKER_FRONT_CENTER, SL_BYTEORDER_LITTLEENDIAN};
    SLDataSink audioSnk = {&loc_bq, &format_pcm};

3.创建recorder

    const SLInterfaceID id[1] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
    const SLboolean req[1] = {SL_BOOLEAN_TRUE};
     (*engineEngine)->CreateAudioRecorder(engineEngine, &recorderObject, &audioSrc,&audioSnk, 1, id, req);

(*recorderObject)->Realize(recorderObject, SL_BOOLEAN_FALSE);

(*recorderObject)->GetInterface(recorderObject, SL_IID_RECORD, &recorderRecord);

4.获取Record Buffer Queue

(*recorderObject)->GetInterface(recorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,&recorderBufferQueue);

5.Record Buffer Queue注册监听器

(*recorderBufferQueue)->RegisterCallback(recorderBufferQueue, bqRecorderCallback,&cntxt);

6.向Record Buffer Queue压入记录缓存

(*recorderBufferQueue)->Enqueue(recorderBufferQueue, cntxt.pData,RECORDER_FRAMES);

7.开始录音

(*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_RECORDING);

8.回调处理录入的数据,继续压缓存入Record Buffer Queue,实现实时录音处理









0 0
原创粉丝点击