The short video SDK provides upgraded recording features based on basic recording. Apart from all features of basic recording, the short video SDK also supports view recording, also called screen recording. You can combine videos that are collected from multiple sources, such as videos recorded in the view and videos recorded by using the camera.
Supported editions
Edition | Supported |
---|---|
Professional | Yes |
Standard | Yes |
Basic | No |
Terms
This section describes the terms that help you better understand multi-source recording. For more information, see Multi-source recording, Track, and Track layout.
Related classes
Class | Description |
---|---|
AliyunIVideoRecorder | A core class that defines multi-source recording features, including recording, preview settings, effect settings, and callback settings. |
AliyunMultiRecorderCreator | A factory class that is used to create a multi-source recording instance. |
AliyunVideoRecorderConfig | A class that defines recording parameters, including the width and height of a video, encoder type, and frame rate. |
AliyunIVideoCapture | A class that defines video capture configurations, including addition of the camera data collector and partial screen recording collector. |
AliyunICameraCapture | A class that defines camera recording. |
AliyunIViewCapture | A class that defines screen recording. |
OnVideoRecordListener | A class that defines recording listeners, including the recording completion callback, recording progress callback, and recording error callback. |
OnAudioCallBack | A class that defines callbacks for audio data in the PCM format. |
OnFrameCallBack | A class that defines callbacks for data capture, including the callback for the selected preview resolution, the callback for the frame data captured by the camera, and the callback for an error that occurs when the camera fails to be enabled. |
OnPictureCallback | A class that defines callbacks for photo capture and snapshots. |
AliyunIClipManager | A class that manages video clips, for example, deletes video clips or configures the recording duration. |
Configure multi-source recording
Configuration | Step | Description | Sample code |
---|---|---|---|
Basic configurations | 1 | Create and destroy recording instances and configure recording parameters. | Initialize the AliyunIVideoRecorder class and configure parameters |
2 | Configure parameters for screen recording and camera recording. | Configure video sources | |
3 | Configure callbacks. | Configure callbacks | |
4 | Configure a preview view and enable the preview. | Enable preview | |
5 | Start, cancel, and stop recording a video clip. | Start recording | |
6 | Stop recording and generate configuration information. | Stop recording | |
Advanced configurations | 7 | Configure parameters for camera control and video clip management. Parameters for camera control include the camera type and flash mode. Parameters for video clip management include the maximum or minimum recording duration, how to delete a video clip, and how to query the number of video clips. Configure these parameters based on your business requirements. | Configure camera settings and manage video clips |
8 | Configure recording effects such as retouching, filters, and animated stickers based on your business requirements. | Configure effects | |
9 | Configure background music, background image, and speed ramping. | Configure other features |
Initialize the AliyunIVideoRecorder class and configure parameters
Initialize the AliyunIVideoRecorder class, create a recording instance, and configure recording parameters. For more information about the parameters that are used in the code, see Related classes.
// Create a recording instance.
AliyunIVideoRecorder recorder = AliyunMultiRecorderCreator.getVideoRecorderInstance(context, config);
// Destroy the recording instance.
// Destroy the recording instance if you no longer need to use the SDK, or before you exit the program. Do not destroy the recording instance during usage. Call the relevant API operation in Activity#onDestroy.
recorder.destroy();
AliyunVideoRecorderConfig config = AliyunVideoRecorderConfig.builder()
// Required parameters:
.videoWidth(***)
.videoHeight(***)
.outputPath(***)
// Optional parameters:
.***()
.***()
.build();
Configure video sources
Combine videos from multiple sources based on your business requirements. After you
add all video sources, you must call recorder.prepare()
to indicate that the video sources are added.
Configure camera recording
// 1. Configure the layout.
AliyunLayoutParam cameraLayoutParam = AliyunLayoutParam.builder()
.layoutLevel(***)
.centerX(***)
.centerY(***)
.widthRatio(***)
.heightRatio(***)
.displayMode(***)
.build();
// 2. Add the video source.
AliyunICameraCapture cameraCapture = recorder.getVideoCapture().addCameraCapture(cameraLayoutParam);
// 3. Configure the video source.
// (Required) Configure the camera preview view.
cameraCapture.setDisplayView(SurfaceView)
// (Optional) Configure other settings.
cameraCapture.set***();
Configure screen recording
// 1. Configure the layout.
AliyunLayoutParam viewLayoutParam = AliyunLayoutParam.builder()
.layoutLevel(***)
.centerX(***)
.centerY(***)
.widthRatio(***)
.heightRatio(***)
.displayMode(***)
.build();
// 2. Add the video source.
// Obtain the current recording view.
View recordView = getRecordView();
AliyunIViewCapture viewCapture = recorder.getVideoCapture().addViewCapture(viewLayoutParam, recordView);
// 3. (Optional) Configure the video source.
viewCapture.set***();
Prepare video sources
recorder.prepare();
Configure callbacks
You can configure callbacks to obtain the processing progress and status of audio and video in a timely manner. For more information about the parameters that are used in the code, see Related classes.
// (Optional) Configure the recording callback.
recorder.setOnRecordListener(OnVideoRecordListener);
// (Optional) Configure the audio collection callback.
recorder.setOnAudioCallback(OnAudioCallBack);
Enable preview
In most cases, call startPreview in Activity#onResume and stopPreview in Activity#onPause. For more information about the parameters that are used in the code, see Related classes.
// Start preview.
// Call the following API operation in Activity#onResume.
AliyunIRecorder.startPreview();
// Stop preview.
// Call the following API operation in Activity#onPause.
AliyunIRecorder.stopPreview();
Start recording
In the recording process, you may need to stop, cancel, or re-record a video until you obtain the complete video that you want. If you stop recording, a video clip is generated. However, if you cancel recording, the current video clip is not retained. For more information about the parameters that are used in the code, see Related classes.
// Start recording.
recorder.startRecording();
// Start recording.
recorder.startRecording();
// Stop recording. A video clip is generated.
recorder.stopRecording();
recorder.startRecording();
// Cancel recording. The current video clip is not saved.
recorder.cancelRecording();
// Continue to record the next video clip.
recorder.startRecording();
recorder.stopRecording();
Stop recording
If you stop recording, the recorded video clips are merged into a video or the configuration information about the recorded video clips is generated. For more information about the parameters that are used in the code, see Related classes.
// Stop recording and merge the recorded video clips into one video.
recorder.finishRecording();
// Stop recording and generate the configuration information about the recorded video clips without merging the video clips.
recorder.finishRecordingForEdit();
Configure camera settings and manage video clips
Configure parameters for camera control and video clip management. Parameters for camera control include the camera type and flash mode. Parameters for video clip management include the maximum or minimum recording duration, how to delete a video clip, and how to query the number of video clips. Configure these parameters based on your business requirements. For more information about the parameters that are used in the code, see Related classes.
// Obtain the number of cameras.
AliyunICameraCapture.getCameraCount();
// Specify the camera type: front camera or rear camera.
AliyunICameraCapture.setCamera(CameraType cameraType);
// Specify the angle of the sensor.
// This setting is very important. We recommend that you read the SDK references.
AliyunICameraCapture.setRotation(int rotation);
// Specify the angle at which a video is recorded.
// This setting is very important. We recommend that you read the SDK references.
AliyunICameraCapture.setRecordRotation(int rotation);
// Configure the camera parameters during preview, including the flash mode, focus mode, zoom factor, and exposure level. You can also configure the preview parameters separately by using the following methods.
AliyunICameraCapture.setCameraParam(CameraParam cameraParam);
// Switch between the front and rear cameras.
AliyunICameraCapture.switchCamera();
// Specify the flash mode.
AliyunICameraCapture.setLight(FlashType flashType);
// Specify the zoom factor.
AliyunICameraCapture.setZoom(float rate);
// Specify the exposure level.
AliyunICameraCapture.setExposureCompensationRatio(float value);
// Specify the focus mode.
AliyunICameraCapture.setFocusMode(int mode);
// Configure manual focus.
AliyunICameraCapture.setFocus(float xRatio, float yRatio);
// Obtain the clip manager.
AliyunIClipManager manager = AliyunIVideoRecorder.getClipManager();
// Specify the maximum recording duration, which is the maximum duration of all video clips instead of a single video clip.
manager.setMaxDuration(int maxDurationMs);
// Specify the minimum recording duration, which is the minimum duration of all video clips instead of a single video clip.
manager.setMinDuration(int minDurationMs);
// Delete the last video clip.
manager.deletePart();
// Delete the specified video clip.
manager.deletePart(int index);
// Delete all video clips.
manager.deleteAllPart();
// Obtain the total duration of video clips.
manager.getDuration();
// Obtain the total number of video clips.
manager.getPartCount();
// Obtain the paths of video clips.
manager.getVideoPathList();
Configure effects
Configure recording effects such as retouching, filters, and animated stickers based on your business requirements. For more information about the parameters that are used in the code, see Related classes.
Filters
You can create custom filters. For more information, see Filters and transitions.
// Apply a filter.
AliyunICameraCapture.applyFilter(EffectFilter effectFilter);
// Remove a filter.
AliyunICameraCapture.removeFilter();
// Apply an animated filter.
AliyunICameraCapture.applyAnimationFilter(EffectFilter effectFilter);
// Remove an animated filter.
AliyunICameraCapture.removeAnimationFilter(EffectFilter effctFilter);
// Add a static sticker.
AliyunICameraCapture.addImage(EffectImage effctImage);
// Remove a static sticker.
AliyunICameraCapture.removeImage(EffectImage effctImage);
// Update the position of a static sticker.
AliyunICameraCapture.setEffectView(float xRatio,float yRatio,float widthRatio,float heightRatio,EffectBase effectBase);
Animated stickers
// Add an animated sticker.
AliyunICameraCapture.addPaster(EffectPaster effectPaster,float sx,float sy,float sw,float sh,float rotation,boolean flip);
// Remove an animated sticker.
AliyunICameraCapture.removePaster(EffectPaster effectPaster);
// Update the position of an animated sticker.
AliyunICameraCapture.setEffectView(float xRatio,float yRatio,float widthRatio,float heightRatio,EffectBase effectBase);
Advanced retouching
- Built-in retouching
// Enable or disable retouching. AliyunICameraCapture.setBeautyStatus(boolean on); // Configure the retouching level. AliyunICameraCapture.setBeautyLevel(int level);
- Retouching SDKs
To use effects provided by a retouching SDK in the short video SDK, you must first obtain the permissions on the retouching SDK and integrate the retouching SDK into the short video SDK.
- For more information about how to integrate Alibaba Cloud Queen SDK, see Queen SDK. For more information about the sample code for configuring effects, see Sample code.
- For more information about how to purchase, integrate, and use FaceUnity, see FaceUnity.
The camera texture ID and the camera raw frame data are required for retouching SDKs to implement retouching effects. The following code provides an example on how to obtain the camera texture ID and the camera raw frame data.- Obtain the camera texture ID
AliyunICameraCapture.setOnTextureIdCallback(new OnTextureIdCallBack() { @Override public int onTextureIdBack(int textureId, int textureWidth, int textureHeight, float[] matrix) { if (mBeautyInterface != null) { return mBeautyInterface.onTextureIdBack(textureId, textureWidth, textureHeight, matrix, mControlView.getCameraType().getType()); } return textureId; } @Override public int onScaledIdBack(int scaledId, int textureWidth, int textureHeight, float[] matrix) { return scaledId; } @Override public void onTextureDestroyed() { // For a version of the short video SDK earlier than V3.7.8, you can destroy gl resources for third-party custom rendering by using GLSurfaceView.queueEvent in GLSurfaceView. For the short video SDK V3.7.8 and later, we recommend that you destroy gl resources in this callback. if (mBeautyInterface != null) { mBeautyInterface.release(); mBeautyInterface = null; } } });
- Obtain the camera raw frame data
AliyunICameraCapture.setOnFrameCallback(new OnFrameCallBack() { @Override public void onFrameBack(byte[] bytes, int width, int height, Camera.CameraInfo info) { // The data type of the callback raw data is NV21. The obtained raw data is used by FaceUnity. if (mBeautyInterface != null) { mBeautyInterface.onFrameBack(bytes, width, height, info); } } @Override public Camera.Size onChoosePreviewSize(List<Camera.Size> supportedPreviewSizes, Camera.Size preferredPreviewSizeForVideo) { return null; } @Override public void openFailed() { } });
Configure other features
Features such as background music, watermarks, and speed ramping are supported. For more information about the parameters that are used in the code, see Related classes.
// Configure speed ramping for recording.
AliyunIVideoRecorder.setRate(float rate);
// Mute the microphone.
AliyunIVideoRecorder.setMute(boolean isMute);
// Specify whether to automatically clear the recorded video clips when you exit recording.
AliyunIVideoRecorder.setIsAutoClearClipVideos(boolean isAutoClear);
// Configure the background music.
AliyunIVideoRecorder.setMusic(String path,long startTime,long duration);
// Remove the background music.
AliyunIVideoRecorder.removeMusic();
// Add a watermark.
AliyunIVideoRecorder.addWaterMark(EffectImage effectImage);
// Remove a watermark.
AliyunIVideoRecorder.removeWaterMark(EffectImage effectImage);
// Specify the background color.
AliyunIVideoRecorder.setBackgroundColor(int color);
// Specify a bitmap background image.
AliyunIVideoRecorder.setBackgroundImage(Bitmap bitmap);
// Specify the path of the background image.
AliyunIVideoRecorder.setBackgroundImage(String path);
// Clear the background.
AliyunIVideoRecorder.clearBackground();
Sample code for multi-source recording
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.aliyun.svideosdk.common.struct.common.AliyunLayoutParam;
import com.aliyun.svideosdk.common.struct.common.VideoDisplayMode;
import com.aliyun.svideosdk.common.struct.encoder.VideoCodecs;
import com.aliyun.svideosdk.common.struct.recorder.CameraType;
import com.aliyun.svideosdk.multirecorder.AliyunICameraCapture;
import com.aliyun.svideosdk.multirecorder.AliyunIVideoRecorder;
import com.aliyun.svideosdk.multirecorder.AliyunIViewCapture;
import com.aliyun.svideosdk.multirecorder.OnVideoRecordListener;
import com.aliyun.svideosdk.multirecorder.config.AliyunVideoRecorderConfig;
import com.aliyun.svideosdk.multirecorder.impl.AliyunMultiRecorderCreator;
import java.io.File;
/**
* Demo sample code for multi-source recording
* Note: You must obtain the storage, camera, and microphone permissions in advance.
*/
public class MultiRecorderDemo extends AppCompatActivity {
private AliyunIVideoRecorder mRecorder;
private TextView mBtnRecord;
private SurfaceView mCameraPreview;
private View mViewRecord;
private View mViewRecordIcon;
private boolean mIsRecording = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multi_recoder_demo_layout);
initViews();
initRecorder();
}
@Override
protected void onResume() {
super.onResume();
mRecorder.startPreview();
}
@Override
protected void onPause() {
super.onPause();
stopRecording();
mRecorder.stopPreview();
}
@Override
protected void onDestroy() {
super.onDestroy();
mRecorder.destroy();
}
private String getSaveDir() {
String saveDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "MultiRecord";
File dirFile = new File(saveDir);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
return saveDir;
}
private void initViews() {
mBtnRecord = findViewById(R.id.record_btn);
mCameraPreview = findViewById(R.id.multi_record_camera);
mViewRecord = findViewById(R.id.multi_record_view);
mViewRecordIcon = mViewRecord.findViewById(R.id.record_view_icon);
}
private void initRecorder() {
// 1. Configure parameters.
AliyunVideoRecorderConfig config = AliyunVideoRecorderConfig.builder()
// Required parameters:
.videoWidth(1080)
.videoHeight(1920)
.outputPath(getSaveDir() + File.separator + System.currentTimeMillis() + ".mp4")
// Optional parameters:
.videoCodecs(VideoCodecs.H264_HARDWARE) // Hardware encoding.
.build();
// 2. Create a recording instance.
mRecorder = AliyunMultiRecorderCreator.getVideoRecorderInstance(this, config);
// 3. Configure the video sources.
// 3.1 Configure camera recording.
addCameraCapture();
// 3.2 Configure screen recording.
addViewCapture();
// 3.3 Prepare the video sources.
mRecorder.prepare();
// 4 Configure callbacks.
mRecorder.setOnRecordListener(new OnVideoRecordListener() {
@Override
public void onProgress(long duration) {
}
@Override
public void onFinish(String outputPath) {
}
@Override
public void onClipComplete(boolean validClip, long clipDuration) {
}
@Override
public void onMaxDuration() {
runOnUiThread(new Runnable() {
@Override
public void run() {
stopRecording();
}
});
}
@Override
public void onError(int errorCode) {
runOnUiThread(new Runnable() {
@Override
public void run() {
stopRecording();
}
});
}
@Override
public void onInitReady() {
}
});
}
private void addCameraCapture() {
// 1. Configure the layout on the left.
AliyunLayoutParam cameraLayoutParam = AliyunLayoutParam.builder()
.layoutLevel(1)
.centerX(0.25f)
.centerY(0.5f)
.widthRatio(0.5f)
.heightRatio(1.0f)
.displayMode(VideoDisplayMode.FILL)
.build();
// 2. Add the video source.
AliyunICameraCapture cameraCapture = mRecorder.getVideoCapture().addCameraCapture(cameraLayoutParam);
// 3. Configure the video source.
// (Required) Configure the camera preview view.
cameraCapture.setDisplayView(mCameraPreview);
// (Optional) Configure other settings.
cameraCapture.setCamera(CameraType.BACK);// The rear camera.
}
private void addViewCapture() {
// 1. Configure the layout on the right.
AliyunLayoutParam viewLayoutParam = AliyunLayoutParam.builder()
.layoutLevel(2)
.centerX(0.75f)
.centerY(0.5f)
.widthRatio(0.5f)
.heightRatio(1.0f)
.displayMode(VideoDisplayMode.FILL)
.build();
// 2. Add the video source.
// Obtain the current recording view.
View recordView = mViewRecord;
AliyunIViewCapture viewCapture = mRecorder.getVideoCapture().addViewCapture(viewLayoutParam, recordView);
// 3. (Optional) Configure the video source.
}
private void startRecording() {
if (mIsRecording) {
return;
}
mIsRecording = true;
mRecorder.startRecording();
}
private void stopRecording() {
if (!mIsRecording) {
return;
}
mRecorder.stopRecording();
mIsRecording = false;
}
public void onClickRecord(View view) {
if (mIsRecording) {
stopRecording();
mBtnRecord.setText("Start recording");
} else {
startRecording();
mBtnRecord.setText("Stop recording");
}
}
}
Configuration example in XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/demo_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<SurfaceView
android:id="@+id/multi_record_camera"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/multi_record_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="@+id/record_view_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Screen recording" />
</FrameLayout>
</LinearLayout>
<Button
android:id="@+id/record_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:onClick="onClickRecord"
android:text="Start recording" />
</RelativeLayout>