Agora Java API Reference for Android

Classes | Public Member Functions | Static Public Attributes | List of all members
io.agora.rtc2.AgoraMediaRecorder Class Reference

## Classes
class | MediaRecorderConfiguration
| Audio and video stream recording configuration. More...

## Public Member Functions
| AgoraMediaRecorder (RtcEngineImpl engine, @NonNull RecorderStreamInfo info)
int | setMediaRecorderObserver (IMediaRecorderCallback callback)
| Registers an `IMediaRecorderCallback` observer. More...
int | startRecording (MediaRecorderConfiguration config)
| Starts recording audio and video streams. More...
int | stopRecording ()
| Stops recording audio and video streams. More...
void | release ()

## Static Public Attributes
static final int | RECORDER_STATE_ERROR = -1
static final int | RECORDER_STATE_START = 2
static final int | RECORDER_STATE_STOP = 3
static final int | RECORDER_REASON_NONE = 0
static final int | RECORDER_REASON_WRITE_FAILED = 1
static final int | RECORDER_REASON_NO_STREAM = 2
static final int | RECORDER_REASON_OVER_MAX_DURATION = 3
static final int | RECORDER_REASON_CONFIG_CHANGED = 4
static final int | STREAM_TYPE_AUDIO = 0x1
static final int | STREAM_TYPE_VIDEO = 0x2
static final int | STREAM_TYPE_BOTH = STREAM_TYPE_AUDIO | STREAM_TYPE_VIDEO
static final int | CONTAINER_MP4 = 1

## Detailed Description

The `AgoraMediaRecorder` class, for recording the audio and video on the client. `AgoraMediaRecorder` can record the following content:

- The audio captured by the local microphone and encoded in AAC format by the SDK.
- The video captured by the local camera and encoded by the SDK.

Since
  v3.5.2

Note
  In the `COMMUNICATION` channel profile, this function is unavailable when there are users using versions of the SDK earlier than v3.0.0 in the channel.

## Member Function Documentation

## ◆ setMediaRecorderObserver()

int io.agora.rtc2.AgoraMediaRecorder.setMediaRecorderObserver | ( | IMediaRecorderCallback | callback | ) |

Registers an `IMediaRecorderCallback` observer.

Since
  v4.0.0

This method sets the callback for audio and video recording, so that the app can be notified of the recording status and information of the audio and video streams during the recording process. Before calling this method, make sure that:

- The `RtcEngine` object has been created and initialized.
- The audio and video recording object has been created via `createMediaRecorder`.

Parameters

  callback | The callback for audio and video stream recording. See `IMediaRecorderCallback` for details.

Returns
  - 0: The method call succeeds.
  - < 0: The method call fails. See `Error Codes` for details and troubleshooting suggestions.

## ◆ startRecording()

int io.agora.rtc2.AgoraMediaRecorder.startRecording | ( | MediaRecorderConfiguration | config | ) |

Starts recording audio and video streams.

This method starts recording audio and video streams. The Agora SDK supports recording both local and remote users' audio and video streams simultaneously. Before starting the recording, make sure that:

- You have created a media recorder object using `createMediaRecorder`.
- You have registered a 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.

After recording starts, if the video resolution changes during the recording process, the SDK stops the recording. If the audio sample rate or number of channels changes, the SDK continues recording and generates a single MP4 file. A recording file is successfully generated only when recordable audio or video streams are detected. If no recordable stream is available, or if the audio or video stream is interrupted for more than 5 seconds during recording, the SDK stops the recording and triggers the `onRecorderStateChanged` (`RECORDER_STATE_ERROR, RECORDER_REASON_NO_STREAM`) callback.

Note
  - If you want to record the local audio and video streams, make sure the local user role is set to broadcaster before starting the recording.
  - If you want to record remote users' audio and video streams, make sure you have subscribed to the remote users' audio and video streams before starting the recording.

Parameters

  config | The configuration for audio and video stream recording. See `MediaRecorderConfiguration`.

Returns
  - 0: The method call succeeds.
  - < 0: The method call fails. See `Error Codes` for details and troubleshooting.
    - -2: Invalid parameter. Make sure that:
      - The specified file path for saving the recording is correct and writable.
      - The specified recording file format is correct.
      - The maximum recording duration is set correctly.
    - -4: The current state of `RtcEngine` does not support this operation. This may occur if recording is already in progress or if it stopped due to an error.
    - -7: The method is called before `RtcEngine` is initialized. Make sure you have created the `AgoraMediaRecorder` object before calling this method.

## ◆ stopRecording()

int io.agora.rtc2.AgoraMediaRecorder.stopRecording | ( |  | ) |

Stops recording audio and video streams.

Note
  After calling `startRecording`, if you want to stop the recording, you must call this method; otherwise, the generated recording file may not play properly.

Returns
  - 0: The method call succeeds.
  - < 0: The method call fails:
    - -7: The method is called before the `RtcEngine` is initialized. Make sure that the `Recorder` object has been created before calling this method.

## Member Data Documentation

## ◆ RECORDER_STATE_ERROR

final int io.agora.rtc2.AgoraMediaRecorder.RECORDER_STATE_ERROR = -1
| static

-1: An error occurs during the recording. See `error` message for the reason.

## ◆ RECORDER_STATE_START

final int io.agora.rtc2.AgoraMediaRecorder.RECORDER_STATE_START = 2
| static

2: The audio and video recording is started.

## ◆ RECORDER_STATE_STOP

final int io.agora.rtc2.AgoraMediaRecorder.RECORDER_STATE_STOP = 3
| static

3: The audio and video recording is stopped.

## ◆ RECORDER_REASON_NONE

final int io.agora.rtc2.AgoraMediaRecorder.RECORDER_REASON_NONE = 0
| static

0: No error occurs.

## ◆ RECORDER_REASON_WRITE_FAILED

final int io.agora.rtc2.AgoraMediaRecorder.RECORDER_REASON_WRITE_FAILED = 1
| static

1: The SDK fails to write the recorded data to a file.

## ◆ RECORDER_REASON_NO_STREAM

final int io.agora.rtc2.AgoraMediaRecorder.RECORDER_REASON_NO_STREAM = 2
| static

2: The SDK does not detect audio and video streams to be recorded, or audio and video streams are interrupted for more than five seconds during recording.

## ◆ RECORDER_REASON_OVER_MAX_DURATION

final int io.agora.rtc2.AgoraMediaRecorder.RECORDER_REASON_OVER_MAX_DURATION = 3
| static

3: The recording duration exceeds the upper limit.

## ◆ RECORDER_REASON_CONFIG_CHANGED

final int io.agora.rtc2.AgoraMediaRecorder.RECORDER_REASON_CONFIG_CHANGED = 4
| static

4: The recording configuration changes.

## ◆ STREAM_TYPE_AUDIO

final int io.agora.rtc2.AgoraMediaRecorder.STREAM_TYPE_AUDIO = 0x1
| static

1: Record audio only.

## ◆ STREAM_TYPE_VIDEO

final int io.agora.rtc2.AgoraMediaRecorder.STREAM_TYPE_VIDEO = 0x2
| static

2: Record video only.

## ◆ STREAM_TYPE_BOTH

final int io.agora.rtc2.AgoraMediaRecorder.STREAM_TYPE_BOTH = STREAM_TYPE_AUDIO | STREAM_TYPE_VIDEO
| static

3: Record both audio and video.

## ◆ CONTAINER_MP4

final int io.agora.rtc2.AgoraMediaRecorder.CONTAINER_MP4 = 1
| static

1: MP4 format.