目录

Using OpenSL ES for Audio Playback (C/C++)

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>.

Using OHAudio to Replace OpenSL ES

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.

OpenSL ES on HarmonyOS

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.

Complete Sample Code

[h2]Linking the Dynamic Library in the CMake Script

target_link_libraries(sample PUBLIC libOpenSLES.so)

Refer to the sample code below to play an audio file.

  1. Add the header files.

    #include "SLES/OpenSLES.h"
    #include "SLES/OpenSLES_OpenHarmony.h"
    #include "SLES/OpenSLES_Platform.h"
  2. 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);
  3. Obtain the engine interface handle corresponding to SL_IID_ENGINE.

    SLEngineItf engineEngine = nullptr;
    (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
  4. 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);
  5. Obtain the bufferQueueItf instance of the SL_IID_OH_BUFFERQUEUE API.

    SLOHBufferQueueItf bufferQueueItf;
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_OH_BUFFERQUEUE, &bufferQueueItf);
  6. 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);
  7. 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);
  8. Stop playing.

    (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
    (*pcmPlayerObject)->Destroy(pcmPlayerObject);
    (*engineObject)->Destroy(engineObject);