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 |
| Starts the local recording. Configures recording parameters and begins the recording. |
|
| 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:
You have a valid Alibaba Cloud account and have created an ApsaraVideo Real-time Communication application. For more information, see Create an application. Obtain the App ID and App Key from the Application Management console.
You have integrated the Alibaba Real-Time Communication (ARTC) SDK into your project and implemented basic real-time audio and video features. For information about SDK integration, see Download and integrate the SDK. To implement audio and video features, see Implement an audio and video call.
Implementation
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: seconds2. 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.
Ensure that the application has the required storage permissions, such as
WRITE_EXTERNAL_STORAGEon Android.On low-performance devices, lower the recording quality to avoid affecting the call experience.