harmony 鸿蒙OpenSL ES Audio Recording Development
OpenSL ES Audio Recording Development
Introduction
You can use OpenSL ES to develop the audio recording function in OpenHarmony. Currently, only some OpenSL ES APIs are implemented. If an API that has not been implemented is called, SL_RESULT_FEATURE_UNSUPPORTED will be returned.
How to Develop
To use OpenSL ES to develop the audio recording function in OpenHarmony, perform the following steps:
Add the header files.
#include <OpenSLES.h> #include <OpenSLES_OpenHarmony.h> #include <OpenSLES_Platform.h>
Use the slCreateEngine API to create and instantiate the engine instance.
SLObjectItf engineObject = nullptr; slCreateEngine(&engineObject, 0, nullptr, 0, nullptr, nullptr); (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
Obtain the engineEngine instance of the SL_IID_ENGINE interface.
SLEngineItf engineItf = nullptr; result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineItf);
Configure the recorder information (including the input source audiosource and output source audiosink), and create a pcmCapturerObject instance.
SLDataLocator_IODevice io_device = { SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, SL_DEFAULTDEVICEID_AUDIOINPUT, NULL }; SLDataSource audioSource = { &io_device, NULL }; SLDataLocator_BufferQueue buffer_queue = { SL_DATALOCATOR_BUFFERQUEUE, 3 }; // Configure the parameters based on the audio file format. SLDataFormat_PCM format_pcm = { SL_DATAFORMAT_PCM, OHOS::AudioStandard::AudioChannel::MONO, OHOS::AudioStandard::AudioSamplingRate::SAMPLE_RATE_44100, OHOS::AudioStandard::AudioSampleFormat::SAMPLE_S16LE, 0, 0, 0 }; SLDataSink audioSink = { &buffer_queue, &format_pcm }; SLObjectItf pcmCapturerObject = nullptr; result = (*engineItf)->CreateAudioRecorder(engineItf, &pcmCapturerObject, &audioSource, &audioSink, 0, nullptr, nullptr); (*pcmCapturerObject)->Realize(pcmCapturerObject, SL_BOOLEAN_FALSE);
Obtain the recordItf instance of the SL_IID_RECORD interface.
SLRecordItf recordItf; (*pcmCapturerObject)->GetInterface(pcmCapturerObject, SL_IID_RECORD, &recordItf);
Obtain the bufferQueueItf instance of the SL_IID_OH_BUFFERQUEUE interface.
SLOHBufferQueueItf bufferQueueItf; (*pcmCapturerObject)->GetInterface(pcmCapturerObject, SL_IID_OH_BUFFERQUEUE, &bufferQueueItf);
Register the BufferQueueCallback function.
static void BufferQueueCallback(SLOHBufferQueueItf bufferQueueItf, void *pContext, SLuint32 size) { AUDIO_INFO_LOG("BufferQueueCallback"); FILE *wavFile = (FILE *)pContext; if (wavFile != nullptr) { SLuint8 *buffer = nullptr; SLuint32 pSize = 0; (*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, pSize); if (buffer != nullptr) { fwrite(buffer, 1, pSize, wavFile); (*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, size); } } return; } // Set wavFile_ to the descriptor of the file to be recorded. (*bufferQueueItf)->RegisterCallback(bufferQueueItf, BufferQueueCallback, wavFile_);
Start audio recording.
static void CaptureStart(SLRecordItf recordItf, SLOHBufferQueueItf bufferQueueItf, FILE *wavFile) { AUDIO_INFO_LOG("CaptureStart"); (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING); if (wavFile != nullptr) { SLuint8* buffer = nullptr; SLuint32 pSize = 0; (*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, pSize); if (buffer != nullptr) { AUDIO_INFO_LOG("CaptureStart, enqueue buffer length: %{public}lu.", pSize); fwrite(buffer, 1, pSize, wavFile); (*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, pSize); } else { AUDIO_INFO_LOG("CaptureStart, buffer is null or pSize: %{public}lu.", pSize); } } return; }
Stop audio recording.
static void CaptureStop(SLRecordItf recordItf) { AUDIO_INFO_LOG("Enter CaptureStop"); fflush(wavFile_); (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED); (*pcmCapturerObject)->Destroy(pcmCapturerObject); fclose(wavFile_); wavFile_ = nullptr; return; }
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Audio Capture Development
harmony 鸿蒙Audio Interruption Mode Development
harmony 鸿蒙Audio Playback Development
harmony 鸿蒙Audio Recording Development
harmony 鸿蒙Audio Rendering Development
harmony 鸿蒙Audio Routing and Device Management Development
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦