The short video SDK provides the basic recording feature and allows you to apply recording effects such as background music, speed ramping, and face stickers to short videos.

Supported editions

Edition Supported
Professional Yes
Standard Yes
Basic Yes

Related classes

Class Description
AliyunIRecorder A core class that defines recording features, including recording, preview settings, effect settings, and callback settings.
AliyunRecorderCreator A factory class that is used to create a recording instance.
MediaInfo A class that defines recording parameters, including the width and height of a video, encoder type, and frame rate.
RecordCallback A class that defines recording callbacks, including the recording completion callback, recording progress callback, and recording error callback.
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.
OnAudioCallBack A class that defines callbacks for audio data in the PCM format.
AliyunIClipManager A class that manages video clips, for example, deletes video clips or configures the recording duration.

Configure recording

Note The camera and microphone permissions are required to record videos.
Configuration Step Description Sample code
Basic configurations 1 Create and destroy recording instances and configure recording parameters. Initialize the AliyunIRecorder class and configure parameters
2 Configure callbacks. Configure callbacks
3 Start and stop preview. Enable preview
4 Start, cancel, and stop recording a video clip. Start recording
5 Stop recording and generate configuration information. Stop recording
Advanced configurations 6 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
7 Configure recording effects such as retouching, filters, and background music based on your business requirements. Configure effects
8 Configure photo capture or facial recognition. Configure other features

Initialize the AliyunIRecorder class and configure parameters

Initialize the AliyunIRecorder class, create a recording instance, and configure recording parameters. For more information about the parameters that are used in the code, see Related classes.

Initialize the class
// Create a recording instance.
AliyunIRecorder recorder = AliyunRecorderCreator.getRecorderInstance(context);// The context parameter indicates the context of the current screen.

// 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.
recorder.destroy();
Configure recording parameters
// Specify the quality of the recorded video.
recorder.setVideoQuality(quality);

// Specify the bitrate of the recorded video.
recorder.setVideoBitrate(int bitrate);// Unit: Kbit/s.

// Configure output parameters for the recorded video.
recorder.setMediaInfo(mediaInfo);// For more information about the parameters, see MediaInfo in the SDK references.

// Specify the output path of the recorded video.
recorder.setOutputPath(string path);

// Specify the output group of pictures (GOP) of the recorded video.
recorder.setGop(int gop);// Unit: frames.

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.

// Configure the recording callback.
recorder.setRecordCallBack(RecordCallback callBack);

// Configure the callback for video frame collection.
recorder.setOnFrameCallback(OnFrameCallBack callback);

// Configure the audio collection callback.
recorder.setOnAudioCallback(OnAudioCallBack callback);

Enable preview

During preview, you must configure SurfaceView. At the same time, perform startPreview in onResume() of Activity/Fragment, and perform stopPreview in onPause(). For more information about the parameters that are used in the code, see Related classes.

// Configure the preview View.
recorder.setDisplayView(SurfaceView displayView);

// Start preview.
recorder.startPreview();

// Stop preview.
// Perform stopPreview in onPause() of Activity/Fragment.
recorder.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
// Start recording.
recorder.startRecording();
Record a video clip
// 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.

Configure camera settings
// Obtain the number of cameras.
AliyunIRrecorder.getCameraCount();

// Specify the camera type.
AliyunIRecorder.setCamera(cameraType);

// Specify whether to mute the audio during recording.
AliyunIRecorder.setMute(boolean isMute);

// Specify the angle of the sensor.
// This setting is very important. We recommend that you read the SDK references. 
AliyunIRecorder.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. 
AliyunIRecorder.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.
AliyunIRecorder.setCameraParam(CameraParam cameraParam);

// Switch between the front and rear cameras.
AliyunIRecorder.switchCamera();

// Specify the flash mode.
AliyunIRecorder.setLight(flashType);

// Specify the zoom factor. 
AliyunIRecorder.setZoom(float rate);

// Specify the exposure level. 
AliyunIRecorder.setExposureCompensationRatio(float value);

// Specify the focus mode. 
AliyunIRecorder.setFocusMode(int mode);

// Configure manual focus. 
AliyunIRecorder.setFocus(float xRatio, float yRatio);
Manage video clips
// Obtain the clip manager.
AliyunIClipManager manager = AliyunIRecorder.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 background music 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.
AliyunIRecorder.applyFilter(EffectFilter effectFilter);// If the path is set to null, the filter is removed.

// Remove a filter.
AliyunIRecorder.applyFilter(new EffectFilter(null));
Animated filters
// Apply an animated filter.
AliyunIRecorder.applyAnimationFilter(EffectFilter effectFilter);

// Remove an animated filter.
AliyunIRecorder.removeAnimationFilter(EffectFilter effctFilter);
Background music
// Configure the background music.
AliyunIRecorder.setMusic(String path,long startTime,long duration);

// Remove the background music.
AliyunIRecorder.setMusic(null, 0, 0);
Speed ramping
// Configure speed ramping for recording.
AliyunIRecorder.setRate(float rate);
Static stickers and static watermarks
// Add a static watermark or a static sticker.
AliyunIRecorder.addImage(EffectImage effctImage);

// Remove a static watermark or a static sticker.
AliyunIRecorder.removeImage(EffectImage effctImage);

// Update the position of a static watermark or a static sticker.
AliyunIRecorder.setEffectView(float xRatio,float yRatio,float widthRatio,float heightRatio,EffectBase effectBase);

Animated stickers

You can create custom animated stickers. For more information, see Animations.
// Add an animated sticker.
AliyunIRecorder.addPaster(EffectPaster effectPaster,float sx,float sy,float sw,float sh,float rotation,boolean flip);

// Remove an animated sticker.
AliyunIRecorder.removePaster(EffectPaster effectPaster);

// Update the position of an animated sticker.
AliyunIRecorder.setEffectView(float xRatio,float yRatio,float widthRatio,float heightRatio,EffectBase effectBase);

Advanced retouching

The video recording module not only provides basic built-in retouching features, but also supports the use of retouching SDKs, such as Alibaba Cloud Queen SDK and FaceUnity. You can use the built-in retouching features to configure only the retouching level. The retouching SDKs provide abundant retouching effects, such as face retouching, face shaping, makeup, filters, and stickers.
  • Built-in retouching
    // Enable or disable retouching.
    AliyunIRecorder.setBeautyStatus(boolean on);
    
    // Configure the retouching level.
    AliyunIRecorder.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
      AliyunIRecorder.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
      AliyunIRecorder.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

Photo capture and facial recognition are supported during video recording. For more information about the parameters that are used in the code, see Related classes.

Photo capture

You can take a photo with effects enabled or by using the default camera settings. The default camera settings do not support effects. After a photo is taken, the data is returned by using RecordCallback.onPictureBack(Bitmap) or RecordCallback.onPictureDataBack(byte[]).
// Take a photo with effects enabled.
AliyunIRecorder.takePhoto(boolean needBitmap);

// Take a photo by using the default camera settings. The default camera settings do not support effects.
AliyunIRecorder.takePicture(boolean needBitmap);

// Specify the size of a photo. This method is supported only if you take a photo by using the default camera settings.
AliyunIRecorder.setPictureSize(Camera.Size size);

Facial recognition

If you want to use facial recognition, you must access the built-in facial recognition model of your application. You can download the Facial recognition model demo for reference.
// Specify whether to enable facial recognition.
AliyunIRecorder.needFaceTrackInternal(boolean need);

// Specify the path of the facial recognition model file.
AliyunIRecorder.setFaceTrackInternalModelPath(String path);

// Specify the angle at which facial recognition is performed.
// This setting is very important. We recommend that you read the SDK references. 
AliyunIRecorder.setFaceDetectRotation(int rotation);

// Specify the number of faces that can be recognized.
// Specify the maximum number of faces that can be recognized at the same time. Maximum value: 3.
AliyunIRecorder.setFaceTrackInternalMaxFaceCount(int maxFaceCount);

// Add an animated face sticker.
AliyunIRecorder.addPaster(EffectPaster effectPaster);

Sample code for basic recording

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.aliyun.svideosdk.common.struct.recorder.MediaInfo;
import com.aliyun.svideosdk.recorder.AliyunIRecorder;
import com.aliyun.svideosdk.recorder.RecordCallback;
import com.aliyun.svideosdk.recorder.impl.AliyunRecorderCreator;
import com.svideo.guide.R;
import java.io.FileOutputStream;

/**
 * Sample code for video recording
 */
class RecordActivity : AppCompatActivity() {

    enum class RecordStatus {
        Idle,
        Recording
    }

    private lateinit var mAliyunRecord : AliyunIRecorder
    private lateinit var mCameraViiew : SurfaceView
    private lateinit var mRecordBtn : ImageView
    private var mRecordStatus = RecordStatus.Idle

    companion object {
        const val TAG = "RecordActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_record)

        mCameraViiew = findViewById(R.id.cameraPreviewView)
        mRecordBtn = findViewById(R.id.btnRecordControl)
        // Start recording.
        mRecordBtn.setOnClickListener {
            if(mRecordStatus == RecordStatus.Recording) {
                mAliyunRecord.stopRecording()
                Toast.makeText(this@RecordActivity, "Stop recording", Toast.LENGTH_SHORT).show()
                updateRecordStatus(RecordStatus.Idle)
            } else {

                mAliyunRecord.takePhoto(true)
            }

        }

        mRecordBtn.setOnLongClickListener {
            if(mRecordStatus == RecordStatus.Idle) {
                val curTime = System.currentTimeMillis()
                mAliyunRecord.setOutputPath("/storage/emulated/0/DCIM/Camera/svideo_record_video_$curTime.mp4")
                mAliyunRecord.startRecording()
                Toast.makeText(this@RecordActivity, "Start recording", Toast.LENGTH_SHORT).show()
                updateRecordStatus(RecordStatus.Recording)
            }
            true
        }

        mAliyunRecord = AliyunRecorderCreator.getRecorderInstance(this)
        val mediaInfo = MediaInfo()
        mediaInfo.fps = 30
        mediaInfo.crf = 6
        mediaInfo.videoWidth = 720
        mediaInfo.videoHeight = 1080

        mAliyunRecord.setMediaInfo(mediaInfo)
        mAliyunRecord.setDisplayView(mCameraViiew)

        mAliyunRecord.setRecordCallBack(object : RecordCallback {
            override fun onComplete(validClip: Boolean, clipDuration: Long) {
                Log.i(TAG, "onComplete")
            }

            override fun onFinish(outputPath: String?) {
                Log.i(TAG, "onFinish path : $outputPath")
            }

            override fun onProgress(duration: Long) {
            }

            override fun onMaxDuration() {
                Log.i(TAG, "onMaxDuration")
            }

            override fun onError(errorCode: Int) {
                Log.i(TAG, "onError : $errorCode")
            }

            override fun onInitReady() {
                Log.i(TAG, "onInitReady")
            }

            override fun onDrawReady() {
                Log.i(TAG, "onDrawReady")
            }

            override fun onPictureBack(bitmap: Bitmap?) {
                Log.i(TAG, "onPictureBack")
                val curTime = System.currentTimeMillis()
                val outputFile = "/storage/emulated/0/DCIM/Camera/svideo_record_photo_$curTime.png"
                val outputStream = FileOutputStream(outputFile)
                bitmap?.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
                outputStream.flush()
                outputStream.close()

                runOnUiThread {
                    Toast.makeText(this@MixRecordActivity, "Pictures saved to the album", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onPictureDataBack(p0: ByteArray?) {
                Log.i(TAG, "onPictureDataBack")
            }

        })



    }

    private fun updateRecordStatus(recordStatus: RecordStatus)
    {
        mRecordStatus = recordStatus
        when(recordStatus) {
            RecordStatus.Idle -> {
                mRecordBtn.setImageResource(R.mipmap.alivc_svideo_bg_record_start)
            }
            RecordStatus.Recording -> {
                mRecordBtn.setImageResource(R.mipmap.alivc_svideo_bg_record_storp)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mAliyunRecord.startPreview()
    }

    override fun onPause() {
        super.onPause()
        mAliyunRecord.stopPreview()
    }

    override fun onDestroy() {
        super.onDestroy()
        mAliyunRecord.destroy()
    }
}

Configuration example in XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <SurfaceView
        android:id="@+id/cameraPreviewView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </SurfaceView>

    <ImageView
        android:id="@+id/btnRecordControl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="50dp"
        android:src="@mipmap/alivc_svideo_bg_record_storp"
        android:visibility="visible">

    </ImageView>


</androidx.constraintlayout.widget.ConstraintLayout>