harmony 鸿蒙OpenSL ES Audio Playback Development
OpenSL ES Audio Playback Development
Introduction
You can use OpenSL ES to develop the audio playback 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 playback 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 obtain an 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 engineEngine = nullptr; (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
Configure the player and create an AudioPlayer instance.
SLDataLocator_BufferQueue slBufferQueue = { SL_DATALOCATOR_BUFFERQUEUE, 0 }; // Configure the parameters based on the audio file format. SLDataFormat_PCM pcmFormat = { SL_DATAFORMAT_PCM, 2, 48000, 16, 0, 0, 0 }; SLDataSource slSource = {&slBufferQueue, &pcmFormat}; SLObjectItf pcmPlayerObject = nullptr; (*engineEngine)->CreateAudioPlayer(engineEngine, &pcmPlayerObject, &slSource, null, 0, nullptr, nullptr); (*pcmPlayerObject)->Realize(pcmPlayerObject, SL_BOOLEAN_FALSE);
Obtain the bufferQueueItf instance of the SL_IID_OH_BUFFERQUEUE interface.
SLOHBufferQueueItf bufferQueueItf; (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_OH_BUFFERQUEUE, &bufferQueueItf);
Open an audio file and register the BufferQueueCallback function.
FILE *wavFile_ = nullptr; static void BufferQueueCallback (SLOHBufferQueueItf bufferQueueItf, void *pContext, SLuint32 size) { FILE *wavFile = (FILE *)pContext; if (!feof(wavFile)) { SLuint8 *buffer = nullptr; SLuint32 pSize = 0; (*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, pSize); // Read data from the file. fread(buffer, 1, size, wavFile); (*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, size); } return; } // Set wavFile_ to the descriptor of the file to be played. wavFile_ = fopen(path, "rb"); (*bufferQueueItf)->RegisterCallback(bufferQueueItf, BufferQueueCallback, wavFile_);
Obtain the playItf instance of the SL_PLAYSTATE_PLAYING interface and start playback.
SLPlayItf playItf = nullptr; (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_PLAY, &playItf); (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
Stop audio playback.
(*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); (*pcmPlayerObject)->Destroy(pcmPlayerObject); (*engineObject)->Destroy(engineObject);
你可能感兴趣的鸿蒙文章
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框自动聚焦