Client-side local recording

Updated at:
Copy as MD

Record audio and video streams on the client side using the ARTC SDK and save them as local files for later playback.

How it works

During a call, you can record local audio and video streams using two APIs:

API

Description

Parameters

startRecord

Starts a local recording session with the specified parameters.

  • recordType: The recording type. Valid values: audio-only or audio and video.

  • recordFormat: The format of the recording file. For audio-only recording, AAC and WAV are supported. For audio and video recording, MP4 is supported.

  • filePath: The path where the recording file is saved.

  • audioConfig: Audio recording settings, such as sample rate, quality, and options for recording audio from external capture or playback sources.

  • videoConfig: Video recording settings, such as video quality and encoding mode.

  • maxSize: The maximum file size in bytes. Recording stops automatically when this limit is reached.

  • maxDuration: The maximum recording duration in seconds. Recording stops automatically when this limit is reached.

stopRecord

Stops the local recording and saves the 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 start, make sure you have completed the following:

Implementation

image

1. Configure recording settings

Before calling startRecord, publish a stream first. After a state change callback confirms successful publication, configure the recording parameters.

Android

AliRtcEngine.AliRtcRecordType recordType = AliRtcEngine.AliRtcRecordType.AliRtcRecordTypeVideo;  // Set the record type: audio-only or audio and video.
AliRtcEngine.AliRtcRecordFormat recordFormat = AliRtcEngine.AliRtcRecordFormat.AliRtcRecordFormatMP4; // Set the recording format. Audio-only: AAC, WAV; Audio and video: MP4.
String filePath = getApplicationContext().getExternalFilesDir( null) + "/record"; // Specify the save path.
Log.i("startRecord", "filePath:" + filePath);
// Configure audio recording settings.
AliRtcEngine.AliRtcRecordAudioConfig audioConfig = new AliRtcEngine.AliRtcRecordAudioConfig();
audioConfig.sampleRate = AliRtcEngine.AliRtcAudioSampleRate.AliRtcAudioSampleRate_48000; // Sample rate.
audioConfig.quality = AliRtcEngine.AliRtcAudioQuality.AliRtcAudioQualityHigh;  // Higher quality results in a larger file size.
audioConfig.externalPcmCaptureRecording = true;  // Enables recording from an external capture source.
audioConfig.externalPcmRenderRecording = true;   // Enables recording from an external render (playback) source.

// Configure video recording settings.
AliRtcEngine.AliRtcRecordVideoConfig videoConfig = new AliRtcEngine.AliRtcRecordVideoConfig();
videoConfig.quality = AliRtcEngine.AliRtcVideoQuality.AliRtcVideoQualityDefault;
videoConfig.encodeMode = AliRtcEngine.AliRtcRecordVideoEncodeMode.AliRtcRecordReusingEncoderMode;  // Reuse the encoder used for publishing.

long maxSize = -1; // Maximum file size in bytes. Recording stops automatically when this limit is reached.
long maxDuration = -1; // Maximum recording duration in seconds. Recording stops automatically when this limit is reached.

iOS

// Set the record type. This example records both audio and video.
let recordType:AliRtcRecordType = .video // Includes both audio and video, supports MP4 format.
// Set the recording format.
let recordFormat: AliRtcRecordFormat = .MP4
// Set the path to save the recording file (app 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)
}

// Configure audio recording settings based on your needs.
var audioConfig = AliRtcRecordAudioConfig(
    sampleRate: ._48000,                    // Sample rate: 48000 Hz.
    quality: .high,                         // High audio quality.
    enableRecordExternalRenderPCM: true,    // Allows recording audio from a custom render (playback) source.
    enableRecordExternalCapturePCM: true    // Allows recording audio from a custom capture source.
)
// Configure video recording settings.
var videoConfig = AliRtcRecordVideoConfig(
    quality: .default,
    sourceType: .video,
    canvas: AliRtcRecordVideoCanvasConfig(canvasWidth: 720, canvasHeight: 1280),
    fps: 30,
    bitrate: 1200
)
// Set limits for file size and duration.
let maxSize: Int64 = -1     // Unit: bytes.
let maxDuration: Int32 = -1 // Unit: seconds.

macOS

/*
   Configure audio settings
*/
    AliRtcRecordAudioConfig AudioConfig ;
    AudioConfig.quality = AliRtcAudioQualityMidium ;
    AudioConfig.sampleRate = AliRtcAudioSampleRate_48000 ;
    AudioConfig.enableRecordExternalCapturePCM = false ;
    AudioConfig.enableRecordExternalRenderPCM = false ;

/*
   Configure video settings
*/
    AliRtcRecordVideoConfig VideoConfig ;
    VideoConfig.sourceType = AliRtcVideosourceCameraType ;
    VideoConfig.bitrate = view.bitrate ;
    VideoConfig.fps = 20 ;
    VideoConfig.quality = AliRtcVideoQualityDefault ;
        

Windows

AliEngineRecordFormat recordFormat = AliEngineRecordFormatMP4; // Recording format. Audio: AAC, WAV; Video: MP4.
std::string filePath = "D:\\record_tmp\\record_video.mp4"; // Save path.

// Audio recording configuration.
AliEngineRecordAudioConfig audioConfig;
audioConfig.sampleRate = AliEngineAudioSampleRate_48000; // Sample rate.
audioConfig.quality = AliEngineAudioQualityHigh;  // Higher quality results in a larger file size.
audioConfig.externalPcmCaptureRecording = true;  // Enables recording from an external capture source.
audioConfig.externalPcmRenderRecording = true;   // Enables recording from an external render (playback) source.

// Video recording configuration.
AliEngineRecordVideoConfig videoConfig;
videoConfig.quality = AliEngineVideoQualityDefault;
// The current version only supports reusing the encoder used for publishing.
videoConfig.encodeMode = AliEngineRecordVideoReusingEncoderMode;  

AliEngineRecordType recordType = AliEngineRecordTypeVideo;

2. Start recording

Call startRecord with your configured settings. On success, the SDK begins recording the audio and video streams.

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
        )
    })
})

macOS

 BOOL ret = [self.engine startRecord:AliRtcRecordTypeVideo recordFormat:AliRtcRecordFormatMP4
                    filePath:view.recordFilePath audioConfig:&AudioConfig videoConfig:&VideoConfig isFragment:FALSE] ;

Windows

mAliRtcEngine->StartRecord(recordType, recordFormat, filePath.c_str(), audioConfig, videoConfig, false);

3. Stop recording

To stop the recording manually, call the stopRecord method.

Android

mAliRtcEngine.stopRecord();

iOS

rtcEngine?.stopRecord()

macOS

[self.engine stopRecord];

Windows

mAliRtcEngine->StopRecord();

4. Retrieve the recording file

After the recording stops, the file is saved to the specified path. You can then upload it to a server, play it back, or delete it.

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

  • On low-performance devices, consider reducing the recording quality to maintain a smooth call experience.