目录

Low-Latency Audio Playback (C/C++)

Starting from API version 10, low-latency audio playback is available.

Low-latency audio playback is an audio rendering solution that leverages software-hardware-chip synergy. By reducing buffer sizes and optimizing the data read/write architecture, this mode delivers significantly lower audio playback latency.

Prerequisites

Development Guidelines

The examples below are code snippets. You can obtain the complete sample via the link at the bottom right of each code example.

[h2]Overview

To use the low-latency mode, you need to develop audio playback by referring to (Recommended) Using OHAudio for Audio Playback (C/C++).

Currently, OHAudio supports two modes: normal mode (AUDIOSTREAM_LATENCY_MODE_NORMAL) and low-latency mode (AUDIOSTREAM_LATENCY_MODE_FAST).

[h2]Setting the Low-Latency Mode

You can specify which mode the audio stream should use by calling OH_AudioStreamBuilder_SetLatencyMode() to set OH_AudioStream_LatencyMode.

The following is an example of setting the low-latency mode:

OH_AudioStream_LatencyMode latencyMode = AUDIOSTREAM_LATENCY_MODE_FAST;
OH_AudioStreamBuilder_SetLatencyMode(builder, latencyMode);

The following sample is provided to help you better understand how to develop audio playback with OHAudio:

Precautions

[h2]Low-Latency Mode Restrictions

The system uses the normal mode under the following conditions, even if the low-latency mode is set:

Starting from API version 20, low-latency query APIs are available:

The playback speed cannot be adjusted in low-latency mode.

[h2]Ensuring Timely Data Supply

In low-latency mode, the application must supply data more frequently than in normal playback mode. Delays in data transmission may cause audio artifacts such as noise. Avoid time-consuming operations in the data callback thread to ensure it returns promptly.

[h2]Data Callback Thread

Audio data to be played is written through the callback API. Implement the callback API and call OH_AudioStreamBuilder_SetRendererWriteDataCallback to set the callback function OH_AudioRenderer_OnWriteDataCallback. This function is used to write audio data and supported since API version 12.

For details about the sample code for developing audio playback, please refer to (Recommended) Using OHAudio for Audio Playback (C/C++).

The following is an example of setting the data callback function:

// Customize a function to write data.
static OH_AudioData_Callback_Result MyOnWriteData_New(
    OH_AudioRenderer* renderer,
    void* userData,
    void* audioData,
    int32_t audioDataSize)
{
    // Write the data to be played to audioData by audioDataSize.
    // If you do not want to play a segment of audioData, return AUDIO_DATA_CALLBACK_RESULT_INVALID.
    size_t readCount = fread(audioData, audioDataSize, 1, g_fp);
    if (readCount == 0) {
        return AUDIO_DATA_CALLBACK_RESULT_INVALID;
    }
    if (feof(g_fp)) {
        fseek(g_fp, 0, SEEK_SET);
    }
    return AUDIO_DATA_CALLBACK_RESULT_VALID;
}
// ...
    // Configure the callback function for writing audio data.
    OH_AudioRenderer_OnWriteDataCallback writeDataCb = MyOnWriteData_New;
    OH_AudioStreamBuilder_SetRendererWriteDataCallback(builder, writeDataCb, nullptr);