All Products
Search
Document Center

:Android

Last Updated:Jun 20, 2026

This topic describes how to integrate the ApsaraVideo Player SDK with the Native RTS SDK on Android to enable RTS.

SDK integration

Add the ApsaraVideo Player SDK and Native RTS SDK dependencies using one of the following methods:

  • Method 1: Maven integration

    1. In your project's app/build.gradle file, add the dependencies for the ApsaraVideo Player SDK and the Native RTS SDK.

      implementation 'com.aliyun.sdk.android:AlivcArtc:5.4.4.0'
      implementation 'com.aliyun.rts.android:RtsSDK:2.1.0'
      implementation 'com.aliyun.sdk.android:AliyunPlayer:5.4.4.0-full'
      Note
      • After you add the AlivcArtc and RtsSDK dependencies, the ApsaraVideo Player SDK automatically loads the RTS SDK as a plug-in.

      • The Maven dependency version provided here is for reference only. For the latest version of the ApsaraVideo Player SDK, see Android SDK Release History. For the latest version of the RTS SDK, see RTS streaming on Android.

      • Ensure that the versions of the ApsaraVideo Player SDK and the Native RTS SDK are compatible. For more information, see SDK Version Compatibility.

    2. Add the Maven repository URLs.

      Add these URLs to the build.gradle file in your project's root directory.

      // The Maven repository for MPChart. You can remove this if you do not need charts.
      maven { url 'https://jitpack.io' }
      // The Maven repository for Alibaba Cloud SDKs (ApsaraVideo Player SDK, RTS SDK).
      maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases' }
  • Method 2: Manual AAR integration

    1. Copy the required .aar files to the app/libs directory.aar文件

    2. In the app/build.gradle file, add the following code:

      dependencies{
          implementation fileTree(dir: 'libs', include: ['*.aar'])
      }

SDK API usage

Call the ApsaraVideo Player SDK API to use the RTS feature. For more information about other features of the ApsaraVideo Player SDK, see Advanced features and API reference.

Note
  • The following code snippets are for demonstration only. For the complete source code, see our open-source project.

  • When playing an RTS stream with the ApsaraVideo Player SDK, you cannot call the pause() method to pause the live stream. Instead, you must call stop() to stop the playback, and then call prepare() to restart it.

  • The seek operation is not supported.

  • Load the library

    Add System.loadLibrary("RtsSDK"); to the appropriate Activity.

    static {
        System.loadLibrary("RtsSDK");
    }
  • Create a player

    The AliPlayerFactory class provides two player types: AliPlayer and AliListPlayer. To play a single media source, use AliPlayer. The following code shows how to create an AliPlayer instance:

    AliPlayer aliyunVodPlayer;
    .....
    aliyunVodPlayer = AliPlayerFactory.createAliPlayer(getApplicationContext());
  • Set player listeners

    The player provides various listeners to handle events, such as onPrepared and onCompletion. The following code shows how to set these listeners:

    aliyunVodPlayer.setOnErrorListener(new IPlayer.OnErrorListener() {
        @Override
        public void onError(ErrorInfo errorInfo) {
            // Called when an error occurs.
        }
    });
    aliyunVodPlayer.setOnPreparedListener(new IPlayer.OnPreparedListener() {
        @Override
        public void onPrepared() {
            // Called when the player is prepared.
        }
    });
    aliyunVodPlayer.setOnVideoSizeChangedListener(new IPlayer.OnVideoSizeChangedListener() {
        @Override
        public void onVideoSizeChanged(int width, int height) {
            // Called when the video resolution changes.
        }
    });
    aliyunVodPlayer.setOnRenderingStartListener(new IPlayer.OnRenderingStartListener() {
        @Override
        public void onRenderingStart() {
            // Called when the first frame is rendered.
        }
    });
    aliyunVodPlayer.setOnInfoListener(new IPlayer.OnInfoListener() {
        @Override
        public void onInfo(int type, long extra) {
            // Called for playback information events, such as loop start, buffering progress, or current position.
        }
    });
    aliyunVodPlayer.setOnLoadingStatusListener(new IPlayer.OnLoadingStatusListener() {
        @Override
        public void onLoadingBegin() {
            // Called when buffering starts.
        }
        @Override
        public void onLoadingProgress(int percent, float kbps) {
            // Buffering progress.
        }
        @Override
        public void onLoadingEnd() {
            // Called when buffering finishes.
        }
    });
    aliyunVodPlayer.setOnSubtitleDisplayListener(new IPlayer.OnSubtitleDisplayListener() {
        @Override
        public void onSubtitleShow(long id, String data) {
            // Called when a subtitle is displayed.
        }
        @Override
        public void onSubtitleHide(long id) {
            // Called when a subtitle is hidden.
        }
    });
    aliyunVodPlayer.setOnTrackChangedListener(new IPlayer.OnTrackChangedListener() {
        @Override
        public void onChangedSuccess(TrackInfo trackInfo) {
            // Called when the track or resolution is switched successfully.
        }
        @Override
        public void onChangedFail(TrackInfo trackInfo, ErrorInfo errorInfo) {
            // Called when switching the track or resolution fails.
        }
    });
    aliyunVodPlayer.setOnStateChangedListener(new IPlayer.OnStateChangedListener() {
        @Override
        public void onStateChanged(int newState) {
            // Called when the player state changes.
        }
    });
    aliyunVodPlayer.setOnSnapShotListener(new IPlayer.OnSnapShotListener() {
        @Override
        public void onSnapShot(Bitmap bm, int with, int height) {
            // Called when a snapshot is captured.
        }
    });
  • Create a data source

    The player supports four types of playback sources: VidSts, VidAuth, VidMps, and UrlSource. To use the RTS feature for direct URL playback, you must use the artc:// protocol in the URL when using UrlSource.

    UrlSource urlSource = new UrlSource();
    urlSource.setUri("artc://");
    aliyunVodPlayer.setDataSource(urlSource);
  • Set the display view

    If the playback source contains video, you must set a display view to render the video frames. Both SurfaceView and TextureView are supported. The following example uses a SurfaceView:

    surfaceView = (SurfaceView) findViewById(R.id.playview);
    surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            aliyunVodPlayer.setDisplay(holder);
        }
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            aliyunVodPlayer.surfaceChanged();
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            aliyunVodPlayer.setDisplay(null);
        }
    });
  • Playback control

    You need to create your own UI for playback controls and call the corresponding player methods in button event handlers. The following example shows basic control operations such as play and stop:

    // Prepare the player. In the onPrepared callback, call start() to begin playback.
    aliyunVodPlayer.prepare();
    // Start playback.
    aliyunVodPlayer.start();
    // Stop playback.
    aliyunVodPlayer.stop();
    // Reset the player.
    aliyunVodPlayer.reset();
    // Release the player. Once released, the player instance cannot be reused.
    aliyunVodPlayer.release();
  • Configure playback parameters

    Configure playback parameters to optimize the RTS performance.

    // Get the current configuration.
    PlayerConfig config = mAliyunVodPlayer.getConfig();
    // Set the maximum latency to 1,000 ms. Latency is managed by the ARTC protocol.
    config.mMaxDelayTime = 1000;
    // Set the start buffer and high buffer duration to 10 ms. Buffering is managed by the ARTC protocol.
    config.mHighBufferDuration = 10;
    config.mStartBufferDuration = 10;
    ....// Other settings
    // Apply the configuration to the player.
    mAliyunVodPlayer.setConfig(config);
  • Logging

    // Enable logging
    Logger.getInstance(context).enableConsoleLog(true);
    Logger.getInstance(context).setLogLevel(Logger.LogLevel.AF_LOG_LEVEL_TRACE);
    // Disable logging
    Logger.getInstance(context).enableConsoleLog(false);
    Logger.getInstance(context).setLogLevel(Logger.LogLevel.AF_LOG_LEVEL_NONE);
  • Live stream degradation

    Live stream degradation is a fallback strategy that changes a playback URL with an artc:// prefix to an HTTP-FLV URL. You can then update the player's UrlSource and restart the playback.

    /**
     * Degradation strategy
     */
    private void willBeDemoted() {
        mRtsAliPlayer.stop();
        if (mUrl.startsWith("artc://")) {
            setDataSource("http://xxxx.flv");
            mRtsAliPlayer.prepare();
        }
    }
  • Obtaining TraceId

    The InfoCode.DirectComponentMSG event in the onInfo callback indicates a pass-through message from the underlying RTS component. To obtain the TraceId, parse the extraMsg string of this event. When the message contains code=104 (the trace ID event), you can extract the TraceId by parsing the substring that follows the -sub- delimiter.

    private static final int TRACE_ID_CODE = 104;
    
    // Listen for the player's onInfo callback and parse the RTS event message.
    mRtsAliPlayer.setOnInfoListener(infoBean -> {
        if (infoBean.getCode() == InfoCode.DirectComponentMSG) {
            String extraMsg = infoBean.getExtraMsg();
            parseDirectComponentMSG(extraMsg);
        }
    });
    
    private void parseDirectComponentMSG(String msg) {
        if (msg.contains("code=" + TRACE_ID_CODE)) {
            parseTraceId(msg);
        }
    }
    
    /**
     * Parses the TraceId.
     */
    private void parseTraceId(String msg) {
        String[] split = msg.split("-sub-");
        if (split.length >= 1) {
            mTraceId = "RequestId:" + (split[1].substring(0, split[1].length() - 1));
            mTraceId = mTraceId.replace("\"", "").replace("\\", "");
        }
    }