All Products
Search
Document Center

ApsaraVideo Live:Client-side local recording

Last Updated:Dec 23, 2025

During video calls, live streams, or online classes, users often need to record and save videos for later viewing. This topic describes how to use the client-side software development kit (SDK) to record audio and video and save them as local files.

Features

During a call, you can record local or remote audio and video streams. The implementation primarily involves two APIs:

API

Description

Parameter description

startRecord

Starts the local recording. Configures recording parameters and begins the recording.

  • recordType: The record type. Options include audio-only and audio and video.

  • recordFormat: The format of the recording file. Audio-only recording supports the AAC and WAV formats. Video recording supports the MP4 format.

  • filePath: The path to save the recording file.

  • audioConfig: The audio recording configuration. It includes the sample rate, recording quality, and whether to allow recording of externally captured or played audio.

  • videoConfig: The video recording configuration. It includes the video quality and encoding mode.

  • maxSize: The maximum file size of the recording in bytes. The recording stops automatically when the file size exceeds this limit.

  • maxDuration: The maximum duration of the recording in seconds. The recording stops automatically when the duration exceeds this limit.

stopRecord

Stops the ongoing local recording and saves the recording file.

None.

Sample code

Local recording on Android: Android/ARTCExample/AdvancedUsage/src/main/java/com/aliyun/artc/api/advancedusage/LocalRecord/RecordingActivity.java

Local recording on iOS: iOS/ARTCExample/AdvancedUsage/LocalRecording/RecordingVC.swift

Prerequisites

Before you begin, make sure that you meet the following requirements:

Implementation

image

1. Initialize the recording configuration

Before you call the startRecord API, configure the recording parameters.

Android

AliRtcEngine.AliRtcRecordType recordType = AliRtcEngine.AliRtcRecordType.AliRtcRecordTypeVideo;  // Record type: audio-only or audio and video
AliRtcEngine.AliRtcRecordFormat recordFormat = AliRtcEngine.AliRtcRecordFormat.AliRtcRecordFormatMP4; // Record format. Audio: AAC, WAV. Video: MP4
String filePath = getApplicationContext().getExternalFilesDir( null) + "/record"; // Save path
Log.i("startRecord", "filePath:" + filePath);
// Audio recording configuration
AliRtcEngine.AliRtcRecordAudioConfig audioConfig = new AliRtcEngine.AliRtcRecordAudioConfig();
audioConfig.sampleRate = AliRtcEngine.AliRtcAudioSampleRate.AliRtcAudioSampleRate_48000; // Sample rate
audioConfig.quality = AliRtcEngine.AliRtcAudioQuality.AliRtcAudioQualityHigh;  // Higher audio quality results in a larger file size.
audioConfig.externalPcmCaptureRecording = true;  // Specifies whether to allow recording of externally captured audio.
audioConfig.externalPcmRenderRecording = true;   // Specifies whether to allow recording of audio from an external player.

// Video recording configuration
AliRtcEngine.AliRtcRecordVideoConfig videoConfig = new AliRtcEngine.AliRtcRecordVideoConfig();
videoConfig.quality = AliRtcEngine.AliRtcVideoQuality.AliRtcVideoQualityDefault;
videoConfig.encodeMode = AliRtcEngine.AliRtcRecordVideoEncodeMode.AliRtcRecordReusingEncoderMode;  // Reuse the stream from the stream ingest encoder or re-encode.

long maxSize = -1; // The maximum file size in bytes. The recording stops automatically if this size is exceeded.
long maxDuration = -1; // The maximum recording duration in seconds. The recording stops automatically if this duration is exceeded.

iOS

// Record type: audio-only or audio and video. This example uses audio and video.
let recordType:AliRtcRecordType = .video // Includes audio and video. Supports MP4.
// Record format
let recordFormat: AliRtcRecordFormat = .MP4
// Path to save the recording file (application sandbox directory).
let fileDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let filePath = (fileDirectory as NSString).appendingPathComponent("record")
let fm = FileManager.default
if !fm.fileExists(atPath: filePath) {
    try? fm.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
}

// Audio recording configuration. Configure as needed.
var audioConfig = AliRtcRecordAudioConfig(
    sampleRate: ._48000,                    // Sample rate: 48k
    quality: .high,                         // High audio quality
    enableRecordExternalRenderPCM: true,    // Allow recording of audio from a custom player.
    enableRecordExternalCapturePCM: true    // Allow recording of audio from a custom capture source.
)
// Video recording configuration
var videoConfig = AliRtcRecordVideoConfig(
    quality: .default,
    sourceType: .video,
    canvas: AliRtcRecordVideoCanvasConfig(canvasWidth: 720, canvasHeight: 1280),
    fps: 30,
    bitrate: 1200
)
// Recording file size and duration limits
let maxSize: Int64 = -1     // Unit: bytes
let maxDuration: Int32 = -1 // Unit: seconds

2. Start recording

Use the configured recordConfig object to start the recording feature. After the API call is successful, the SDK will start recording the audio and video streams of the current call based on the configuration.

Android

mAliRtcEngine.startRecord(recordType, recordFormat, filePath, audioConfig, videoConfig, maxSize, maxDuration);

iOS

let result = withUnsafeMutablePointer(to: &audioConfig, { audioPtr in
    withUnsafeMutablePointer(to: &videoConfig, { videoPtr in
        engine.start(
            recordType,
            recordFormat: recordFormat,
            filePath: filePath,
            audioConfig: audioPtr,
            videoConfig: videoPtr
        )
    })
})

3. Stop recording

To stop the recording, call the stopRecord API.

Android

mAliRtcEngine.stopRecord();

iOS

rtcEngine?.stopRecord()

4. Get the recording file

After the recording is complete, the recording file is saved to the specified path. You can then process the file as needed, such as by uploading it to a server, playing it back, or deleting the temporary file.

Note
  • Ensure that the application has the required storage permissions, such as WRITE_EXTERNAL_STORAGE on Android.

  • On low-performance devices, lower the recording quality to avoid affecting the call experience.