当前位置:   article > 正文

OpenHarmony OpenSl ES音频录制_opensl es录音

opensl es录音

1、OpenHarmony OpenSl ES音频录制开发简介

        开发者可以通过本博文了解在OpenHarmony中如何使用OpenSL ES进行录音相关操作;当前仅实现了部分OpenSL ES接口,因此调用未实现接口后会返回。SL_RESULT_FEATURE_UNSUPPORTED。

 2、OpenHarmony OpenSl ES音频录制开发指导

        以下步骤描述了在OpenHarmony如何使用 OpenSL ES 开发音频录音功能:

(1).添加头文件

  1. #include <OpenSLES.h>
  2. #include <OpenSLES_OpenHarmony.h>
  3. #include <OpenSLES_Platform.h>

(2).使用 slCreateEngine 接口创建引擎对象和实例化引擎对象 engine

  1. SLObjectItf engineObject = nullptr;
  2. slCreateEngine(&engineObject, 0, nullptr, 0, nullptr, nullptr);
  3. (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);

(3).获取接口 SL_IID_ENGINE 的引擎接口 engineEngine 实例

  1. SLEngineItf engineItf = nullptr;
  2. result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineItf);

(4).配置录音器信息(配置输入源audiosource、输出源audiosink),创建录音对象pcmCapturerObject

  1. SLDataLocator_IODevice io_device = {
  2. SL_DATALOCATOR_IODEVICE,
  3. SL_IODEVICE_AUDIOINPUT,
  4. SL_DEFAULTDEVICEID_AUDIOINPUT,
  5. NULL
  6. };
  7. SLDataSource audioSource = {
  8. &io_device,
  9. NULL
  10. };
  11. SLDataLocator_BufferQueue buffer_queue = {
  12. SL_DATALOCATOR_BUFFERQUEUE,
  13. 3
  14. };
  15. //具体参数需要根据音频文件格式进行适配
  16. SLDataFormat_PCM format_pcm = {
  17. SL_DATAFORMAT_PCM,
  18. OHOS::AudioStandard::AudioChannel::MONO,
  19. OHOS::AudioStandard::AudioSamplingRate::SAMPLE_RATE_44100,
  20. OHOS::AudioStandard::AudioSampleFormat::SAMPLE_S16LE,
  21. 0,
  22. 0,
  23. 0
  24. };
  25. SLDataSink audioSink = {
  26. &buffer_queue,
  27. &format_pcm
  28. };
  29. SLObjectItf pcmCapturerObject = nullptr;
  30. result = (*engineItf)->CreateAudioRecorder(engineItf, &pcmCapturerObject,
  31. &audioSource, &audioSink, 0, nullptr, nullptr);
  32. (*pcmCapturerObject)->Realize(pcmCapturerObject, SL_BOOLEAN_FALSE);

(5).获取录音接口SL_IID_RECORD 的 recordItf 接口实例

  1. SLRecordItf recordItf;
  2. (*pcmCapturerObject)->GetInterface(pcmCapturerObject, SL_IID_RECORD, &recordItf);

(6).获取接口 SL_IID_OH_BUFFERQUEUE 的 bufferQueueItf 实例

  1. SLOHBufferQueueItf bufferQueueItf;
  2. (*pcmCapturerObject)->GetInterface(pcmCapturerObject, SL_IID_OH_BUFFERQUEUE, &bufferQueueItf);

(7).注册 BufferQueueCallback 回调

  1. static void BufferQueueCallback(SLOHBufferQueueItf bufferQueueItf, void *pContext, SLuint32 size)
  2. {
  3. AUDIO_INFO_LOG("BufferQueueCallback");
  4. FILE *wavFile = (FILE *)pContext;
  5. if (wavFile != nullptr) {
  6. SLuint8 *buffer = nullptr;
  7. SLuint32 pSize = 0;
  8. (*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, pSize);
  9. if (buffer != nullptr) {
  10. fwrite(buffer, 1, pSize, wavFile);
  11. (*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, size);
  12. }
  13. }
  14. return;
  15. }
  16. //wavFile_ 需要设置为用户想要录音的文件描述符
  17. (*bufferQueueItf)->RegisterCallback(bufferQueueItf, BufferQueueCallback, wavFile_);

(8).开始录音

  1. static void CaptureStart(SLRecordItf recordItf, SLOHBufferQueueItf bufferQueueItf, FILE *wavFile)
  2. {
  3. AUDIO_INFO_LOG("CaptureStart");
  4. (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING);
  5. if (wavFile != nullptr) {
  6. SLuint8* buffer = nullptr;
  7. SLuint32 pSize = 0;
  8. (*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, pSize);
  9. if (buffer != nullptr) {
  10. AUDIO_INFO_LOG("CaptureStart, enqueue buffer length: %{public}lu.", pSize);
  11. fwrite(buffer, 1, pSize, wavFile);
  12. (*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, pSize);
  13. } else {
  14. AUDIO_INFO_LOG("CaptureStart, buffer is null or pSize: %{public}lu.", pSize);
  15. }
  16. }
  17. return;
  18. }

(9). 结束录音

  1. static void CaptureStop(SLRecordItf recordItf)
  2. {
  3. AUDIO_INFO_LOG("Enter CaptureStop");
  4. fflush(wavFile_);
  5. (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
  6. (*pcmCapturerObject)->Destroy(pcmCapturerObject);
  7. fclose(wavFile_);
  8. wavFile_ = nullptr;
  9. return;
  10. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/329308
推荐阅读
相关标签
  

闽ICP备14008679号