OpenSL ES, short for Open Sound Library for Embedded Systems, is an embedded, cross-platform, free audio processing library. It provides standardized, high-performance, low-latency APIs for app developers on embedded mobile multimedia devices. The Native API of HarmonyOS is implemented based on the OpenSL ES 1.0.1 API specification developed by the Khronos Group. Developers can use the relevant APIs on HarmonyOS through <SLES/OpenSLES.h> and <SLES/OpenSLES_OpenHarmony.h>.
HarmonyOS provides the OpenSL ES APIs for audio development at the native layer since SDK8. As the version evolves, these APIs fail to meet the capability expansion requirements of the audio system and therefore are no longer recommended.
Starting from SDK10, HarmonyOS introduces the OHAudio interface, through which all audio capabilities of the system are exposed. The OHAudio interface already covers all the capabilities provided by OpenSL ES on HarmonyOS, and extends support for new features such as audio focus events and low latency.
For details about the OHAudio development guide, please refer to (Recommended) Using OHAudio for Audio Playback (C/C++).
For app developers who adopted HarmonyOS earlier, a reference for switching from OpenSL ES to OHAudio is provided here: Switching from OpenSL ES to OHAudio (C/C++), to help developers migrate to the new interface more quickly in newer versions.
Currently, HarmonyOS implements parts of OpenSL ES APIs to implement basic audio playback functionalities.
Calling an unimplemented API returns SL_RESULT_FEATURE_UNSUPPORTED. Currently, no related extensions are available.
The following lists the OpenSL ES APIs that have been implemented on HarmonyOS. For details, see the OpenSL ES specifications.
SLInterfaceID implemented on HarmonyOS
| SLInterfaceID | Description |
|---|---|
| SL_IID_ENGINE | Universal engine, which provides the interface for creating player objects. |
| SL_IID_PLAY | Provides the player status interface. |
| SL_IID_VOLUME | Provides interfaces for adjusting and reading the volume of audio playback streams. |
| SL_IID_OH_BUFFERQUEUE | Provides the callback registration interface for audio playback stream data. |
Engine APIs implemented on HarmonyOS
Object APIs implemented on HarmonyOS
Playback APIs implemented on HarmonyOS
Volume control APIs implemented on HarmonyOS
BufferQueue APIs implemented on HarmonyOS
The APIs listed below can be used only after <OpenSLES_OpenHarmony.h> is introduced.
| API | Description |
|---|---|
| SLresult (*Enqueue) (SLOHBufferQueueItf self, const void *buffer, SLuint32 size) | Adds a buffer to the corresponding queue. For an audio playback operation, this API adds the buffer with audio data to the filledBufferQ_ queue. For an audio recording operation, this API adds the idle buffer after recording data storage to the freeBufferQ_ queue. The self parameter indicates the BufferQueue object that calls this API. The buffer parameter indicates the pointer to the buffer with audio data or the pointer to the idle buffer after the recording data is stored. The size parameter indicates the size of the buffer. |
| SLresult (*Clear) (SLOHBufferQueueItf self) | Releases a BufferQueue object. The self parameter indicates the BufferQueue object that calls this API. |
| SLresult (*GetState) (SLOHBufferQueueItf self, SLOHBufferQueueState *state) | Obtains the state of a BufferQueue object. The self parameter indicates the BufferQueue object that calls this API. The state parameter indicates the pointer to the state of the BufferQueue object. |
| SLresult (*RegisterCallback) (SLOHBufferQueueItf self, SlOHBufferQueueCallback callback, void* pContext) | Registers a callback. The self parameter indicates the BufferQueue object that calls this API. The callback parameter indicates the callback to be registered for the audio playback or recording operation. The pContext parameter indicates the pointer to the audio file to be played for an audio playback operation or the pointer to the audio file to be recorded for an audio recording operation. |
| SLresult (*GetBuffer) (SLOHBufferQueueItf self, SLuint8** buffer, SLuint32* size) | Obtains a buffer. For an audio playback operation, this API obtains an idle buffer from the freeBufferQ_ queue. For an audio recording operation, this API obtains the buffer that carries recording data from the filledBufferQ_ queue. The self parameter indicates the BufferQueue object that calls this API. The buffer parameter indicates the double pointer to the idle buffer or the buffer carrying recording data. The size parameter indicates the size of the buffer. |
target_link_libraries(sample PUBLIC libOpenSLES.so)
Refer to the sample code below to play an audio file.
Add the header files.
#include "SLES/OpenSLES.h" #include "SLES/OpenSLES_OpenHarmony.h" #include "SLES/OpenSLES_Platform.h"
Use the slCreateEngine API to create and instantiate the engine object.
SLObjectItf engineObject = nullptr; slCreateEngine(&engineObject, 0, nullptr, 0, nullptr, nullptr); (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
Obtain the engine interface handle corresponding to SL_IID_ENGINE.
SLEngineItf engineEngine = nullptr; (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
Configure the player and create an AudioPlayer instance.
SLObjectItf outputMixObject = nullptr;
(*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, nullptr, nullptr);
(*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
SLDataLocator_BufferQueue slBufferQueue = {
SL_DATALOCATOR_BUFFERQUEUE,
1
};
// Configure the parameters based on the audio file format.
SLDataFormat_PCM pcmFormat = {
SL_DATAFORMAT_PCM,
2, // Number of channels.
SL_SAMPLINGRATE_48, // Sampling rate.
SL_PCMSAMPLEFORMAT_FIXED_16, // Audio sample format.
16,
SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT,
SL_BYTEORDER_LITTLEENDIAN
};
SLDataSource slSource = {
&slBufferQueue,
&pcmFormat
};
SLDataLocator_OutputMix slOutputMix = {
SL_DATALOCATOR_OUTPUTMIX,
outputMixObject
};
SLDataSink slSink = {
&slOutputMix,
nullptr
};
SLObjectItf pcmPlayerObject = nullptr;
(*engineEngine)->CreateAudioPlayer(engineEngine,
&pcmPlayerObject,
&slSource,
&slSink,
0,
nullptr,
nullptr);
(*pcmPlayerObject)->Realize(pcmPlayerObject, SL_BOOLEAN_FALSE); Obtain the bufferQueueItf instance of the SL_IID_OH_BUFFERQUEUE API.
SLOHBufferQueueItf bufferQueueItf; (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_OH_BUFFERQUEUE, &bufferQueueItf);
Open an audio file and register the BufferQueueCallback function.
static void BufferQueueCallback (SLOHBufferQueueItf bufferQueueItf, void *pContext, SLuint32 size)
{
SLuint8 *buffer = nullptr;
SLuint32 pSize;
(*bufferQueueItf)->GetBuffer(bufferQueueItf, &buffer, &pSize);
// Write the audio data to be played to the buffer.
(*bufferQueueItf)->Enqueue(bufferQueueItf, buffer, size);
}
void *pContext = nullptr; // Custom context information can be passed in and will be received in the Callback.
(*bufferQueueItf)->RegisterCallback(bufferQueueItf, BufferQueueCallback, pContext); Obtain the playback interface handle corresponding to SL_IID_PLAY and start playback.
SLPlayItf playItf = nullptr; (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_PLAY, &playItf); (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
Stop playing.
(*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); (*pcmPlayerObject)->Destroy(pcmPlayerObject); (*engineObject)->Destroy(engineObject);