Recording

createMediaRecorder

Creates a recorder instance for audio and video recording.

public abstract AgoraMediaRecorder createMediaRecorder(RecorderStreamInfo info);

Before starting to record audio and video streams, you need to call this method to create a recorder instance. You can call this method multiple times to create multiple recorders and specify the channel name and user ID to be recorded through the info parameter. After successfully creating the recorder, you need to call setMediaRecorderObserver to register an observer for the recorder to listen for recording-related callbacks, and then call startRecording to start recording.

Parameters

info
Information about the audio and video stream to be recorded. See RecorderStreamInfo.

Return Values

  • The AgoraMediaRecorder instance, if the method call succeeds.
  • An empty pointer , if the method call fails.

destroyMediaRecorder

Destroys the audio and video recorder instance.

public abstract void destroyMediaRecorder(AgoraMediaRecorder mediaRecorder);

If recording is in progress, call stopRecording to stop recording before calling destroyMediaRecorder to destroy the recorder instance.

Parameters

mediaRecorder
The recorder instance to destroy. See AgoraMediaRecorder.

Return Values

  • 0: Success.
  • < 0: Failure.

setMediaRecorderObserver

Registers a callback observer for audio and video recording.

public int setMediaRecorderObserver(IMediaRecorderCallback callback)
Since
Available since v4.0.0.
Note: Before calling this method, make sure that:

Parameters

callback
Callback for audio and video stream recording. See IMediaRecorderCallback.

Return Values

  • 0: Success.
  • < 0: Failure.

startAudioRecording [1/2]

Starts client-side audio recording.

public abstract int startAudioRecording(String filePath, int quality);

You must call this method after calling joinChannel. The recording automatically stops when leaveChannel is called. Make sure the specified directory exists and is writable.

Note: When calling this method, the default recording sample rate is 32 kHz and cannot be changed.

Parameters

filePath
The absolute path of the recording file to be saved locally, including the file name and extension. For example: /sdcard/emulated/0/audio.aac.
Note: Make sure the specified path exists and is writable.
quality
Recording quality:
  • 0: Low quality. Sample rate is 32 kHz, about 1.2 MB for 10 minutes.
  • 1: Medium quality. Sample rate is 32 kHz, about 2 MB for 10 minutes.
  • 2: High quality. Sample rate is 32 kHz, about 3.75 MB for 10 minutes.

Return Values

  • 0: Success.
  • < 0: Failure.

startAudioRecording [2/2]

Starts client-side recording and applies recording configuration.

public abstract int startAudioRecording(AudioRecordingConfiguration config);
After calling this method, you can record audio from users in the channel and obtain the recording file. The recording file supports the following formats only:
  • WAV: High fidelity but large file size. For example, at 32000 Hz, 10 minutes is about 73 MB.
  • AAC: Lower fidelity but smaller file size. For example, at 32000 Hz and AUDIO_RECORDING_QUALITY_MEDIUM, 10 minutes is about 2 MB.
Recording automatically stops when the user leaves the channel.

Timing

You must call this method after joining the channel.

Parameters

config
Recording configuration. See AudioRecordingConfiguration.

Return Values

  • 0: Success.
  • < 0: Failure.

startRecording

Starts recording audio and video streams.

public int startRecording(MediaRecorderConfiguration config)
This method starts recording audio and video streams. The Agora SDK supports recording both local and remote user streams. Before calling this method, make sure that:
  • You have created the media recorder object using createMediaRecorder.
  • You have registered the media recorder observer using setMediaRecorderObserver to listen for recording-related callbacks.
  • You have joined a channel.
This method supports recording the following data:
  • Audio captured by the microphone, in AAC format.
  • Video captured by the camera, in H.264 or H.265 format.
If the video resolution changes after recording starts, the SDK stops recording. If the audio sample rate or channel count changes, the SDK continues recording and generates an MP4 file.A recording file is generated only when a recordable audio or video stream is detected. If no recordable stream is detected, or if the stream is interrupted for more than 5 seconds during recording, the SDK stops recording and triggers the onRecorderStateChanged (RECORDER_STATE_ERROR, RECORDER_REASON_NO_STREAM) callback.
Note:
  • To record local audio and video streams, make sure the local user role is set to broadcaster before starting the recording.
  • To record remote user streams, make sure to subscribe to the remote user's audio and video streams before starting the recording.

Parameters

config
Configuration for recording audio and video streams. See MediaRecorderConfiguration.

Return Values

  • 0: Success.
  • < 0: Failure.
    • -2: Invalid parameter. Make sure that:
      • The specified path for saving the recording file is correct and writable.
      • The specified recording file format is correct.
      • The maximum recording duration is set correctly.
    • -4: The current RtcEngine state does not support this operation. This may be because recording is already in progress or has stopped due to an error.
    • -7: This method is called before initializing RtcEngine. Make sure to create the AgoraMediaRecorder object before calling this method.

stopAudioRecording

Stops client-side audio recording.

public abstract int stopAudioRecording();

Return Values

  • 0: Success.
  • < 0: Failure.

stopRecording

Stops recording audio and video streams.

public int stopRecording()
Note: After calling startRecording, you must call this method to stop recording; otherwise, the generated recording file may not play properly.

Return Values

  • 0: Success.
  • < 0: Failure:
    • -7: This method is called before initializing RtcEngine. Make sure to create the Recorder instance before calling this method.

onRecorderInfoUpdated

Callback when recording information is updated.

void onRecorderInfoUpdated(String channelId, int uid, RecorderInfo info)

After you successfully register this callback and start recording the audio and video stream, the SDK triggers this callback periodically based on the value of recorderInfoUpdateInterval set in MediaRecorderConfiguration, reporting the current recording file name, duration, and size.

Trigger Timing

This callback is triggered periodically after you successfully register the callback and start recording the audio and video stream, according to the recorderInfoUpdateInterval set in MediaRecorderConfiguration.

Parameters

channelId
Channel name.
uid
User ID.
info
Recording file information. See RecorderInfo.

onRecorderStateChanged

Callback when the recording state changes.

@CalledByNative void onRecorderStateChanged(String channelId, int uid, int state, int reason);

This callback is triggered by the SDK when the recording state of the audio and video stream changes, reporting the current recording state and the reason for the change.

Parameters

channelId
Channel name.
uid
User ID.
state
Current recording state:
  • RECORDER_STATE_ERROR (-1): An error occurred during recording.
  • RECORDER_STATE_START (2): Start recording audio and video streams.
  • RECORDER_STATE_STOP (3): Stop recording audio and video streams.
reason
Reason for the recording state change:
  • RECORDER_REASON_NONE (0): Everything is normal.
  • RECORDER_REASON_WRITE_FAILED (1): Failed to write the recording file.
  • RECORDER_REASON_NO_STREAM (2): No available audio/video stream, or the stream is interrupted for more than 5 seconds.
  • RECORDER_REASON_OVER_MAX_DURATION (3): Recording duration exceeds the maximum limit.
  • RECORDER_REASON_CONFIG_CHANGED (4): Recording configuration changed.