HarmonyOS AVRecorder: Complete Guide to Audio/Video Recording with ArkTS & NDK Code Examples

This guide covers HarmonyOS AVRecorder's supported formats (AAC/MP3, H.264/H.265, MP4/M4A), provides step-by-step ArkTS and C++ NDK code examples for recording workflow, and outlines key development practices including state machine rules, resource cleanup, and permission handling.

HarmonyOS Developer Technology
HarmonyOS Developer Technology
HarmonyOS Developer Technology
HarmonyOS AVRecorder: Complete Guide to Audio/Video Recording with ArkTS & NDK Code Examples

Supported Formats

AVRecorder is HarmonyOS's native audio/video recording component supporting mainstream formats:

Audio encoding: AAC, MP3

Video encoding: H.264/AVC, H.265/HEVC

Container formats: MP4, M4A

Typical applications include smart voice recorders, custom cameras, and home monitoring systems.

Development Languages

ArkTS Development

Enables rapid UI construction for audio/video recording with integrated state listening, error capture, and flow control.

C/C++ NDK Development

Provides deep integration with underlying hardware, suitable for complex projects like camera and audio/video engines, delivering high-performance capture and processing.

Recording Workflow (ArkTS Example)

// 1. Create AVRecorder instance
const avRecorder = await media.createAVRecorder();
// 2. Configure parameters and prepare recording
const avConfig = {
  audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC,
  videoSourceType: media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV,
  profile: {
    audioBitrate: 112000,
    audioChannels: 2,
    audioCodec: media.CodecMimeType.AUDIO_AAC,
    fileFormat: media.ContainerFormatType.CFT_MPEG_4,
    videoBitrate: 200000,
    videoCodec: media.CodecMimeType.VIDEO_AVC,
    videoFrameWidth: 640,
    videoFrameHeight: 480
  },
  url: 'fd://xx'  // Reference app file access example to create and read/write a file
};
await avRecorder.prepare(avConfig);
// 3. Get Surface ID and pass to camera module
await avRecorder.getInputSurface();
// 4. Start/pause/resume/stop recording
avRecorder.start();
avRecorder.pause();
avRecorder.resume();
avRecorder.stop();
// 5. Release resources
avRecorder.reset();
avRecorder.release();

NDK Development (C++ Example)

// 1. Create AVRecorder instance
OH_AVRecorder* avRecorder = OH_AVRecorder_Create();
// 2. Configure parameters and prepare recording
OH_AVRecorder_Config config;
config.audioSourceType = AVRECORDER_MIC;
config.profile.audioBitrate = 96000;
config.profile.audioChannels = 2;
config.profile.audioCodec = AVRECORDER_AUDIO_AAC;
config.profile.audioSampleRate = 48000;
config.profile.fileFormat = AVRECORDER_CFT_MPEG_4;
config.fileGenerationMode = AVRECORDER_APP_CREATE;
config.metadata.location.latitude = 27.791863;
config.metadata.location.longitude = 64.574687;
OH_AVRecorder_Prepare(avRecorder, &config);
// 3. Start camera, get Surface ID, pass to camera module
OH_AVRecorder_GetInputSurface(avRecorder, &window);
// 4. Start/pause/resume/stop recording
OH_AVRecorder_Start(avRecorder);
OH_AVRecorder_Pause(avRecorder);
OH_AVRecorder_Resume(avRecorder);
OH_AVRecorder_Stop(avRecorder);
// 5. Release resources
OH_AVRecorder_Reset(avRecorder);
OH_AVRecorder_Release(avRecorder);

Development Best Practices

Strictly follow state machine rules: Only call pause() in started state; only call resume() in paused state.

Resource release is mandatory: Always call release() after recording ends to avoid memory, thread, and system resource leaks.

Permission management: Request microphone permission in advance when microphone access is needed.

Technical Advantages

Efficient and stable: Provides convenient audio/video recording capabilities to help developers build stable multimedia applications and generate media files.

Native integration: As HarmonyOS's native recording framework, AVRecorder is deeply integrated with the system, offering strong compatibility and fully leveraging device hardware capabilities.

Flexible configuration: Supports customization of various audio/video formats, encoding parameters, and recording modes, allowing developers to adjust settings for different scenarios.

Open documentation and community: Comprehensive official docs, rich sample code, and active developer community support accelerate learning and development efficiency.

Official References

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-media-avrecorder
https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-avrecorder-h
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mobile developmentHarmonyOSArkTSNDKaudio recordingvideo recordingmedia APIAVRecorder
HarmonyOS Developer Technology
Written by

HarmonyOS Developer Technology

HarmonyOS developers provide key technology analysis, version updates, Codelabs practice, and event information for HarmonyOS. Welcome developers to join the HarmonyOS ecosystem and create infinite possibilities together!

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.