Record Single Stream
Feature Overview
The Local Server Recording SDK supports recording a single audio/video stream in a room. After integrating the SDK, you can start using its single-stream recording feature.
Prerequisites
- The ZEGOCLOUD Local Server Recording SDK has been integrated into your project. For details, see Quick Start - Integration.
- You have created a project in the ZEGOCLOUD Console and obtained a valid AppID. For details, see "Project Information" in Console - Project Management.
Steps
Set the event callback listeners
Before initializing the SDK, first call the setZegoRoomCallback and setZegoStreamRecordCallback APIs to set the room callbacks and the recording callbacks respectively, so as to receive the related event notifications during audio/video stream recording.
private static IZegoRoomCallback mRoomCallback = new IZegoRoomCallback() {
//Handle room callbacks
};
private static IZegoStreamRecordCallback mStreamRecordCallback = new IZegoStreamRecordCallback() {
//Handle recording event callbacks
};
public void setZegoRoomCallback(IZegoRoomCallback callback)
public void setZegoStreamRecordCallback(IZegoStreamRecordCallback callback)Before initializing the SDK, you can also call the setLogDirAndSize API as needed to set the log file path and size, which facilitates problem locating and analysis.
Initialize the SDK
Call the initSDK API to initialize the SDK, and pass the obtained AppID into the parameter "appID".
public boolean initSDK(long appID, IZegoInitSDKCompletionCallback callback)Register the asynchronous callback onInitSDK to receive the related event notifications during SDK initialization.
public interface IZegoInitSDKCompletionCallback {
void onInitSDK(int errorCode);
}Set the Token
After initializing the SDK and before logging in to a room, call the setToken API to pass in the Token you obtained for authentication.
For how to obtain the Token, see Authenticate with Token.
public void setToken(String token, String roomID)Log in to the room
After the authentication is passed, call the loginRoom API to log in to the room.
public boolean loginRoom(String roomID, IZegoLoginCompletionCallback callback)Register the asynchronous callback onLoginCompletion to receive the related event notifications during room login.
public interface IZegoLoginCompletionCallback {
void onLoginCompletion(int errorCode, String roomID);
}Start recording
After logging in to the room successfully, call the startRecordSingleStream API to start recording the audio/video stream.
- It is recommended that, after logging in to the room successfully and receiving the onStreamUpdate stream information update callback notification and confirming the information of the added stream, you call the startRecordSingleStream API to start single-stream recording.
- Before starting recording, you can call the getMaxRecordCount API to get the maximum number of streams the SDK supports recording simultaneously, and handle the number of concurrently recorded streams based on its value.
public boolean startRecordSingleStream(String streamID, String pathAndName, int muxerStreamType, int fragmentSeconds, ZegoRecordSingleStreamConfig config, String roomID)Where:
- streamID: The ID of the stream you need to record.
- pathAndName: The storage path of the file generated after recording (including the file name).
- It must end with
.mp3/.aac/.mp4/.flv/.m3u8(.mp3and.aaconly support recording pure audio). - The SDK automatically creates the corresponding path; if a file with the same name already exists in the path, it directly overwrites the original file.
- It must end with
- muxerStreamType: The type of the stream to record. See ZegoMuxerStreamType. By default, both audio and video are recorded.
- fragmentSeconds: The fragment interval of the recording file, in seconds.
- For the m3u8 format, the value range is [2, 60]; for other formats, the value range is [0, 10].
- A value of 0 means no fragmentation; a value greater than 0 means fragmentation; the default value is 2.
- Fragmentation can prevent issues where the saved recording files cannot play properly when recording is interrupted due to exceptions during the recording process.
- config: The various configurations for single-stream recording. For details, see ZegoRecordSingleStreamConfig.
Fragmentation is a logical concept for the SDK to process recording files internally, and does not split the recorded file into multiple small files by interval for storage.
Register the asynchronous callback onStreamRecordBegin to receive the related event notifications during recording.
public interface IZegoStreamRecordCallback {
void onStreamRecordBegin(String streamID, String pathAndName);
}After recording starts successfully, the onStreamRecordBegin callback is received.
Stop recording
When recording is no longer needed or is completed, call the stopRecordSingleStream API to stop recording.
public boolean stopRecordSingleStream(String streamID)Register the asynchronous callback onStreamRecordEnd to receive the related event notifications when recording is stopped.
public interface IZegoStreamRecordCallback {
void onStreamRecordEnd(String streamID, String pathAndName, int reason);
}Then:
- Call the logoutRoom API to exit the room.
- Call the setZegoRoomCallback(null) and setZegoStreamRecordCallback(null) APIs to stop the callback listeners.
- Call the unInitSDK API to uninitialize the SDK.
