All Products
Search
Document Center

ApsaraVideo Live:Implement RTS stream pulling on Android

Last Updated:Jan 14, 2026

This topic describes how to use the ApsaraVideo Player software development kit (SDK) for Android to implement RTS.

SDK integration

Add the ApsaraVideo Player SDK dependency files as follows:

  • Maven integration

    1. Add the Maven repository address.

      Add the Maven repository address to the build.gradle file in the root directory of your project.

      // The Maven repository address for Alibaba Cloud SDKs (ApsaraVideo Player)
      maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases' }
    2. Add the ApsaraVideo Player SDK dependency files to the app/build.gradle file of your project.

      def player_sdk_version = "x.x.x" // Use the latest version.
      def rts_sdk_version = "7.11.0" // Independent version number. This is the latest version.
      
      // The main library of the player.
      implementation "com.aliyun.sdk.android:AliyunPlayer:$player_sdk_version-full"
      
      // The bridge layer (AlivcArtc) between the player and the RTS component. The version number must be the same as the player's version number. This must be integrated with the RTS component.
      implementation "com.aliyun.sdk.android:AlivcArtc:$player_sdk_version"
      
      // The RTS component.
      implementation "com.aliyun.rts.android:RtsSDK:$rts_sdk_version"
      Important
      • The version number of the bridge layer (AlivcArtc) must be the same as the player's version number. You must integrate the bridge layer with the RTS component.

      • For more information about player versions, see SDK download. We recommend using the latest version. The player must be V7.11.0 or later, and the minimum supported version is V5.4.5.0.

      • For answers to common questions, see the Android Player FAQ.

Use the ApsaraVideo Player SDK API

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

Note
  • The following code provides an example. For the complete code, see the RTS Playback module in the API-Example project. This Java-based Android sample project helps developers quickly integrate the core features of the ApsaraVideo Player SDK.

  • When you use ApsaraVideo Player for RTS stream pulling, do not call the pause method to pause the live stream. Instead, call the stop method to stop playback and then call the prepare method to restart playback.

  • The seek (drag) operation is not supported.

Load the RTS library

As needed, add System.loadLibrary("RtsSDK"); to the appropriate Activity.

static {
    System.loadLibrary("RtsSDK");
}

Create the player

  1. Create a player.

    Create an AliPlayer instance using the AliPlayerFactory class.

    // Create a player instance.
    AliPlayer mAliPlayer = AliPlayerFactory.createAliPlayer(context);
    // Pass the traceId.
    // Optional: We recommend that you use the Tracing Analysis feature. When an exception occurs during video playback with the ApsaraVideo Player SDK, you can use this feature to trace the entire link for a specific user or playback session. This helps you quickly diagnose the problem and improve playback experience administration efficiency.
    // Define the traceId value. It must be a unique identifier for your user or their device. For example, pass your business's user ID, or the device ID such as an International Mobile Equipment Identity (IMEI) or identifier for advertisers (IDFA).
    mAliPlayer.setTraceId("traceId");
    Note

    The Playback Quality Monitoring feature (which lets you view data about the overall playback quality), the Tracing Analysis feature (which lets you locate specific users or devices, analyze their playback behavior, and quickly identify issues such as playback abnormalities), and the Video Playback Statistics feature all depend on the instrumentation log reporting feature.

    When you create the player, the available features depend on how you set the setTraceId parameter:

    • If you do not pass the setTraceId parameter (default): The instrumentation log reporting feature is enabled. You can use the playback quality monitoring and video playback statistics features. The Tracing Analysis feature is not available.

    • If you pass a traceId to the setTraceId parameter: You must define the traceId value. The value must be a unique identifier for a user or their device, such as a business user ID, an IMEI, or an IDFA. After you pass the traceId, the instrumentation log reporting feature is enabled. You can then use the playback quality monitoring, Tracing Analysis, and video playback statistics features.

    • If you set the setTraceId parameter to DisableAnalytics: The instrumentation log reporting feature is disabled. You cannot use the playback quality monitoring, Tracing Analysis, or video playback statistics features.

  2. Set the display view.

    The player supports AliDisplayView (recommended), SurfaceView, and TextureView. You can choose one of the following views.

    1. Declare the view in the XML file.

      AliDisplayView (recommended)

      <!-- Player rendering view -->
      <com.aliyun.player.videoview.AliDisplayView
          android:id="@+id/ali_display_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center" />

      SurfaceView

      <!-- Player rendering view -->
      <SurfaceView
          android:id="@+id/surface_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center" />

      TextureView

      <!-- Player rendering view -->
      <TextureView
          android:id="@+id/texture_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center" />
    2. Set the display view.

      AliDisplayView (recommended)

      Call the setDisplayView method of the player to attach the player view.

      AliDisplayView mAliDisplayView = findViewById(R.id.display_view);
      // Set the player view type using setPreferDisplayView().
      mAliDisplayView.setPreferDisplayView(AliDisplayView.DisplayViewType.SurfaceView);
      mAliPlayer.setDisplayView(mAliDisplayView);

      SurfaceView

      Call the setSurface method of the player to attach the player view.

      SurfaceView mSurfaceView = findViewById(R.id.surface_view);
      mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
          @Override
          public void surfaceCreated(@NonNull SurfaceHolder holder) {
              mAliPlayer.setSurface(holder.getSurface());
          }
      
          @Override
          public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
              mAliPlayer.surfaceChanged();
          }
      
          @Override
          public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
              mAliPlayer.setSurface(null)
          }
      });

      TextureView

      Call the setSurface method of the player to attach the player view.

      TextureView mTextureView = findViewById(R.id.texture_view);
      mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
          @Override
          public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
              mAliPlayer.setSurface(new Surface(surface));
          }
      
          @Override
          public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
              mAliPlayer.surfaceChanged();
          }
      
          @Override
          public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
              mAliPlayer.setSurface(null);
              return true;
          }
      
          @Override
          public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
      
          }
      });
  3. Set the playback source.

    The player supports four types of playback sources: VidSts, VidAuth, VidMps, and UrlSource. UrlSource is used for direct URL playback. To use the RTS service, you must set the URL protocol to artc://.

    UrlSource urlSource = new UrlSource();
    urlSource.setUri("artc://<streaming URL>");
    mAliPlayer.setDataSource(urlSource);
    Note

    For more information about how to set the playback source, see Basic features.

  4. Configure playback parameters.

    Configure playback parameters to improve the performance of RTS.

    Note

    Player SDK V6.3.0 and later support automatic optimal configuration for low latency. If the playback URL starts with "artc://" and you have not manually modified the PlayerConfig, mMaxDelayTime, mHighBufferDuration, or mStartBufferDuration values, the SDK automatically uses the values 1000, 10, and 10 for playback.

    To customize the control, see the following example:

    // 1. Get and modify the configuration.
    PlayerConfig config = mAliPlayer.getConfig();
    if (playUrl.startWith("artc://")) {
        // The maximum delay is 1000 ms.
        config.mMaxDelayTime = 1000;
        // The start buffer duration is 10 ms.
        config.mStartBufferDuration = 10;
        // The buffer duration for stuttering recovery is 10 ms.
        config.mHighBufferDuration = 10;
    } else {
        // Use the default configuration for config or customize it.
    }
    
    // 2. Apply the configuration.
    mAliPlayer.setConfig(config);
  5. Start playback.

    // Prepare for playback.
    mAliPlayer.prepare();
    // Start playback.
    mAliPlayer.start();

Control playback

The ApsaraVideo Player SDK for Android supports operations such as starting and stopping playback.

  1. Start playback.

    Start video playback using the start method. The following example shows how to do this:

    mAliPlayer.start();
  2. Stop playback.

    Stop video playback using the stop method. The following example shows how to do this:

    mAliPlayer.stop();
  3. Destroy the player.

    Destroy the player instance. You can destroy the instance in synchronous or asynchronous mode. The following example shows how to do this:

    // Sync destroy. The stop method is automatically called.
    mAliPlayer.release();
    // Asynchronous destroy. The stop method is automatically called.
    mAliPlayer.releaseAsync();	
    Note

    The synchronous destroy method returns a value only after all player resources are released. If your interface requires a fast response, we recommend that you use the asynchronous destroy method. Note the following:

    1. Do not perform any other operations on the player object during the asynchronous destroy process.

    2. You do not need to manually stop the player before you call the asynchronous destroy method. This is because the process already includes an asynchronous stop flow.

Auxiliary features

  1. Enable or disable 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);
  2. RTS stream fallback.

    Note
    • Playback fallback applies only to timeout scenarios. In a playback timeout scenario, if a fallback solution is configured, the player attempts to fall back. Otherwise, the player's onError callback is triggered. Exceptions such as 404 or 403 errors or streamer disconnection do not trigger a fallback.

    • Automatic RTS fallback uses the same domain name to fall back from RTS to FLV. If your RTS and FLV domain names are different, you must configure a custom RTS fallback solution to specify the target FLV domain name.

    1. Automatic RTS fallback (enabled by default)

      When you play a stream from an RTS URL, if a custom RTS fallback is not configured and RTS stream pulling fails, the player automatically falls back to the corresponding default FLV URL for playback. The following example shows how to do this:

      // 1: enabled, 0: disabled. Enabled by default.
      AliPlayerGlobalSettings.setOption(AliPlayerGlobalSettings.ALLOW_RTS_DEGRADE, 1);
    2. Custom RTS fallback

      When you play a stream from an RTS URL, you can set a fallback URL, such as an HLS or FLV URL. If RTS stream pulling fails, the player automatically falls back to this URL for playback. The following example shows how to do this:

      PlayerConfig config = mAliPlayer.getConfig();
      // Optional. Configure other items in config.
      UrlSource urlSource = new UrlSource();
      urlSource.setUri(downgradeUrl);
      // Set the fallback URL.
      mAliPlayer.enableDowngrade(urlSource, config);
  3. Obtain the TraceID.

    Each low-latency playback session has a traceId that you can use for troubleshooting. You can obtain the traceId through the player's event callback.

    // Listen for the player's onInfo callback and parse the DemuxerTraceID message.
    mAliPlayer.setOnInfoListener(infoBean -> {
        if (infoBean.getCode() == InfoCode.DemuxerTraceID) {
            String traceId = infoBean.getExtraMsg();
    });