Mobile Development 12 min read

Mastering Audio & Video Capture and Playback in HarmonyOS Apps

This guide details how to implement audio and video capture using Camera Kit, AudioCapturer, and OHAudio, as well as playback with AVPlayer in HarmonyOS apps, covering permission setup, device selection, profile configuration, session creation, resource management, and supported formats.

360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
Mastering Audio & Video Capture and Playback in HarmonyOS Apps

In HarmonyOS app development, audio and video capabilities are core interaction carriers that support scenarios such as real‑time interaction, remote collaboration, short video, e‑commerce live streaming, and more.

1. Video Capture

The official recommendation is to use the Camera Kit framework, which provides custom focus, exposure control, video stream processing, multi‑camera switching and other features. The typical workflow includes:

Request camera permission in module.json5 and at runtime.

Obtain the list of supported cameras via CameraManager.getSupportedCameras() and select a camera ID.

Retrieve supported profiles (resolution, frame rate, format) through CameraOutputCapability and match them to the desired configuration.

Create a preview session, add input and output streams, and bind a preview Surface obtained from an XComponent.

Start preview or recording; for recording, create a video Surface, let Camera Kit deliver frames to the media recorder, which saves the file.

Release resources when the operation is finished.

Camera workflow
Camera workflow
Camera development model
Camera development model

2. Audio Capture

HarmonyOS offers three APIs for audio recording: AudioCapturer (ArkTS/JS), OpenSL ES (Native) and OHAudio (Native). AudioCapturer is the recommended choice; OHAudio replaces OpenSL ES and adds low‑latency support.

Typical AudioCapturer usage:

Create an AudioCapturer instance with the desired source type (MIC, VOICE_RECOGNITION, etc.) and request the ohos.permission.MICROPHONE permission.

Subscribe to the audio data callback; avoid heavy processing in the callback thread.

Call start() to begin recording.

Call stop() to stop recording.

Call release() to destroy the instance and free resources.

AudioCapturer options
AudioCapturer options

3. Audio & Video Playback

HarmonyOS provides two system players: SoundPool for short sound effects and AVPlayer for full‑featured audio/video playback. AVPlayer decodes common containers (mp4, mkv, ts) and audio formats (aac, mp3, wav, etc.) and supports subtitles (srt, vtt, webvtt).

AVPlayer usage steps:

Create an AVPlayer instance with createAVPlayer().

Register required event listeners.

Set the media source – either a local file descriptor or a network URL (declare ohos.permission.INTERNET for network).

If playing video, obtain a SurfaceID from an XComponent and bind it.

Control playback with play(), pause(), seek(), and stop().

Optionally reset the player with reset() to change the source.

When finished, release the player and destroy resources.

AVPlayer creation
AVPlayer creation

Limitations of AVPlayer include the lack of built‑in local caching (requires third‑party libraries), background‑play restrictions without the Background Tasks Kit, and limited protocol/format support that may need additional adaptation for DRM or special codecs.

HarmonyOSVideo CaptureAVPlayerAudio CaptureCamera Kit
360 Zhihui Cloud Developer
Written by

360 Zhihui Cloud Developer

360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.