This topic describes how to integrate Native RTS SDK for Android with ApsaraVideo Player SDK to use the Real-Time Streaming (RTS) feature in ApsaraVideo Live.

Procedure

  1. Use Maven to add the dependencies of ApsaraVideo Player SDK and Native RTS SDK.

    Add the dependencies of ApsaraVideo Player SDK and Native RTS SDK to the app/build.gradle file of your project.

    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, ApsaraVideo Player SDK automatically loads Native RTS SDK as a plug-in.
    • The SDK versions specified in the preceding Maven dependency code are only for reference. For more information about the latest versions of ApsaraVideo Player SDKs, see Release notes of ApsaraVideo Player SDK for Android. For more information about the latest versions of RTS SDKs, see Download SDKs.
    • Make sure that the requirements are met when you integrate Native RTS SDK with ApsaraVideo Player SDK. For more information, see Release notes.
  2. Add the URLs of Maven repositories.

    Add the URLs of Maven repositories to the build.gradle file in the root directory.

    // The URL of the Maven repository for MPChart. If you do not need MPChart, you can delete this line of code.
    maven { url 'https://jitpack.io' }
    // The URL of the Maven repository for ApsaraVideo Player SDK and RTS SDK.
    maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases' }
  3. Call an API operation of ApsaraVideo Player SDK to use the RTS feature.
    • Create a player

      Use the AliPlayerFactory class to create a player. You can create the following two types of players: AliPlayer and AliListPlayer. If you want to play a single stream, use AliPlayer. You can run the following code to create an AliPlayer:

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

      ApsaraVideo Player SDK for Android provides a variety of listeners, such as onPrepared and onCompletion. You can run the following code to set listeners:

      aliyunVodPlayer.setOnErrorListener(new IPlayer.OnErrorListener() {
          @Override
          public void onError(ErrorInfo errorInfo) {
              // Listens to errors.
          }
      });
      aliyunVodPlayer.setOnPreparedListener(new IPlayer.OnPreparedListener() {
          @Override
          public void onPrepared() {
              // Listens to successful preparation.
          }
      });
      aliyunVodPlayer.setOnVideoSizeChangedListener(new IPlayer.OnVideoSizeChangedListener() {
          @Override
          public void onVideoSizeChanged(int width, int height) {
              // Listens to the change of video resolution.
          }
      });
      aliyunVodPlayer.setOnRenderingStartListener(new IPlayer.OnRenderingStartListener() {
          @Override
          public void onRenderingStart() {
              // Listens to the display of the first frame.
          }
      });
      aliyunVodPlayer.setOnInfoListener(new IPlayer.OnInfoListener() {
          @Override
          public void onInfo(int type, long extra) {
              // Listens to other events, such as the start of loop playback, the buffer position, the current playback position, and the start of autoplay.
          }
      });
      aliyunVodPlayer.setOnLoadingStatusListener(new IPlayer.OnLoadingStatusListener() {
          @Override
          public void onLoadingBegin() {
              // Listens to the start of buffering.
          }
          @Override
          public void onLoadingProgress(int percent, float kbps) {
              // Listens to the buffering progress.
          }
          @Override
          public void onLoadingEnd() {
              // Listens to the end of buffering.
          }
      });
      aliyunVodPlayer.setOnSubtitleDisplayListener(new IPlayer.OnSubtitleDisplayListener() {
          @Override
          public void onSubtitleShow(long id, String data) {
              // Listens to the display of subtitles.
          }
          @Override
          public void onSubtitleHide(long id) {
              // Listens to the hiding of subtitles.
          }
      });
      aliyunVodPlayer.setOnTrackChangedListener(new IPlayer.OnTrackChangedListener() {
          @Override
          public void onChangedSuccess(TrackInfo trackInfo) {
              // Listens to the success of switching between audio and video streams or between resolutions.
          }
          @Override
          public void onChangedFail(TrackInfo trackInfo, ErrorInfo errorInfo) {
              // Listens to the failure of switching between audio and video streams or between resolutions.
          }
      });
      aliyunVodPlayer.setOnStateChangedListener(new IPlayer.OnStateChangedListener() {
          @Override
          public void onStateChanged(int newState) {
              // Listens to the change of player status.
          }
      });
      aliyunVodPlayer.setOnSnapShotListener(new IPlayer.OnSnapShotListener() {
          @Override
          public void onSnapShot(Bitmap bm, int with, int height) {
              // Listens to the capture of snapshots.
          }
      });
    • Configure a playback source

      ApsaraVideo Player supports four types of playback sources: VidSts, VidAuth, VidMps, and UrlSource. UrlSource is used for URL-based playback. If you use UrlSource, you must prefix URLs with artc:// to use the RTS feature.

      UrlSource urlSource = new UrlSource();
      urlSource.setUri("artc://<Streaming URL>");
      aliyunVodPlayer.setDataSource(urlSource);
    • Set the user interface (UI) view

      If the playback source contains video images, you must set the UI view to display the video images. SurfaceView and TextureView are supported. Sample code for 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);
          }
      });
    • Set playback control

      You must create playback control buttons and associate click events with playback control methods to implement playback control. The basic control features include play, stop, pause, and seek. The seek feature is applicable only for ApsaraVideo VOD. If you pause a live stream, the live stream stops at the current position. After you resume the playback of the live stream, the live stream starts from that position. Sample code:

      // Prepares for the playback. You can specify the start() method in the callback so that the playback starts upon the completion of preparation.
      aliyunVodPlayer.prepare();
      // Starts the playback.
      aliyunVodPlayer.start();
      // Pauses the playback.
      aliyunVodPlayer.pause();
      // Stops the playback.
      aliyunVodPlayer.stop();
      // Jumps to an inaccurate position.
      aliyunVodPlayer.seekTo(long position);
      // Resets the player.
      aliyunVodPlayer.reset();
      // Releases the player. The player cannot be used after it is released.
      aliyunVodPlayer.release();
    • Set playback parameters

      You can set playback parameters to improve the RTS effect.

      // Queries the configurations.
      PlayerConfig config = mAliyunVodPlayer.getConfig();
      // Sets the maximum latency to 1 second. The ARTC protocol controls the latency.
      config.mMaxDelayTime = 1000;
      // Sets the buffer period to 10 milliseconds. The ARTC protocol controls the buffer period. 
      config.mHighBufferDuration = 10;
      config.mStartBufferDuration = 10;
      .... // Configure other settings.
      // Loads the settings for the player.
      mAliyunVodPlayer.setConfig(config);
    • Load the library

      Add System.loadLibrary("RtsSDK"); to the required activity based on your actual requirements.

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

    For more information about the features of ApsaraVideo Player SDK, see Implementation and API operations.