The system provides a variety of APIs for you to develop audio playback applications. You can select them based on the audio data formats, audio sources, audio usage scenarios, and even the programming language you use. Selecting a suitable class helps you reduce development workload and your application deliver a better effect.
AudioRenderer: an ArkTS/JS API for audio output. It supports only PCM format and requires the app to continuously write audio data to operate. The app can add data preprocessing before input, such as configuring the sampling rate and bit width of the audio stream. Developers are expected to have basic knowledge of audio processing. It is suitable for developing more professional and diverse media playback apps.
AudioHaptic: provides ArkTS and JS APIs for audio playback with audio-haptic effect. It applies to scenarios where haptic feedback needs to be initiated synchronously during audio playback, for example, when there are incoming calls or messages or users are typing.
OHAudio: a Native API for audio output. This API is designed with a normalized architecture and supports both standard audio paths and low-latency paths. It supports only PCM format and is suitable for scenarios where audio output is implemented at the native layer.
In addition to the preceding classes, you can also use AVPlayer and SoundPool in Media Kit to implement audio playback.
AVPlayer: an ArkTS/JS API for audio playback. It integrates streaming media and local resource parsing, media resource demuxing, audio decoding, and audio output. It can be used to directly play audio files in formats such as MP3 and M4A, but does not support direct playback of PCM files.
SoundPool: provides ArkTS and JS APIs to implement short sound playback in low latency mode. It can be used to play short sound effects, such as camera shutter sound effect, key press sound effect, and game shooting sound effect.
If you want the application to continue playing in the background (including the scenario where the screen is off), you must develop the audio playback functionality and choose either accessing AVSession or requesting a continuous task based on your service scenario. The specific rules are as follows:
When an application needs to play media types (stream types STREAM_USAGE_MUSIC, STREAM_USAGE_MOVIE, and STREAM_USAGE_AUDIOBOOK) and game types (stream type STREAM_USAGE_GAME) in the background, it must access AVSession and request continuous tasks.
In addition to the aforementioned playback types, when an application needs to run other user-perceptible tasks in the background for a long time, it must request continuous tasks of the AUDIO_PLAYBACK type.
If an application does not meet the above access standards, playback in the background will be muted and frozen by the system, preventing normal background playback. Playback will only be resumed and unmuted when the application is brought back to the foreground.
For details, see Background Playback.