This topic describes how to use the ApsaraVideo Player SDK for Android to implement RTS.
Integrate the SDK
Add the dependency files for the ApsaraVideo Player SDK. The following example shows how.
Maven integration
Add the Maven repository address.
Add the Maven repository address to the build.gradle file in the root directory.
// The Maven repository address for Alibaba Cloud SDKs (ApsaraVideo Player) maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases' }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.9.0" // Independent version number. This is the latest version. // The primary 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 that of the player. This component 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"ImportantThe version number of the bridge layer (AlivcArtc) between the player and the RTS component must be the same as the player's version number. The bridge layer must be integrated with the RTS component.
For information about player versions, see SDK download. Use the latest version. The player version must be V5.4.5.0 or later. V7.5.0 or later is recommended.
For more information, see Android Player FAQ.
Use ApsaraVideo Player SDK APIs
Call the ApsaraVideo Player SDK APIs to implement the RTS feature. For more information about other features of the ApsaraVideo Player SDK, see Advanced features and API reference.
The following code provides an example. For the complete code, see the RTS ultra-low latency playback module in the API-Example project. This Java-based Android sample project for the ApsaraVideo Player SDK helps you quickly learn how to integrate the core features of the SDK.
When you use ApsaraVideo Player for RTS stream pulling, do not call `pause` to pause the live stream. Instead, call `stop` to stop the playback and then call `prepare` to restart it.
The seek (drag) operation is not supported.
Load the RTS library
As needed, add System.loadLibrary("RtsSDK"); in the appropriate Activity.
static {
System.loadLibrary("RtsSDK");
}Create a player
Create a player.
Create an AliPlayer instance using the
AliPlayerFactoryclass.// Create a player instance. AliPlayer mAliPlayer = AliPlayerFactory.createAliPlayer(context); // Pass the traceId. // Optional: Use the Tracing Analysis feature. If an exception occurs during video playback with ApsaraVideo Player SDK, you can use this feature to trace the entire playback session for a specific user or device. This helps you quickly diagnose issues and improve the playback experience. // The traceId value is a custom value that must be a unique identifier for your user or their device, such as a user ID, International Mobile Equipment Identity (IMEI), or identifier for advertisers (IDFA). mAliPlayer.setTraceId("traceId");NoteThe player's playback quality monitoring, Tracing Analysis, and video playback statistics features rely on instrumentation log reporting. Playback quality monitoring lets you view overall playback quality data. Tracing Analysis lets you locate specific users or devices, analyze their playback behavior, and quickly identify playback exceptions.
When you create a player, the available features depend on the setting of the
setTraceIdparameter:If the
setTraceIdparameter is not passed (default): Instrumentation log reporting is enabled. You can use the playback quality monitoring and video playback statistics features. However, the Tracing Analysis feature is unavailable.If a traceId is passed to the
setTraceIdparameter: The traceId is a custom value that must be a unique identifier for the user or device, such as a user ID, IMEI, or IDFA. After you pass the traceId, instrumentation log reporting is enabled, and you can use the playback quality monitoring, Tracing Analysis, and video playback statistics features.If the
setTraceIdparameter is set toDisableAnalytics: Instrumentation log reporting is disabled. The playback quality monitoring, Tracing Analysis, and video playback statistics features are all unavailable.
Set the display view.
The player supports AliDisplayView (recommended), SurfaceView, and TextureView. You can choose one of the following views.
Declare the view in an 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" />Set the display view.
AliDisplayView (recommended)
Call the
setDisplayViewAPI of the player to attach the view.AliDisplayView mAliDisplayView = findViewById(R.id.display_view); // Use setPreferDisplayView() to set the playback view type. mAliDisplayView.setPreferDisplayView(AliDisplayView.DisplayViewType.SurfaceView); mAliPlayer.setDisplayView(mAliDisplayView);SurfaceView
Call the
setSurfaceAPI of the player to attach the 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
setSurfaceAPI of the player to attach the 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) { } });
Set the playback source.
The player supports four playback sources: VidSts, VidAuth, VidMps, and UrlSource. UrlSource allows for direct playback from a URL. To use the RTS service, set the URL protocol to
artc://.UrlSource urlSource = new UrlSource(); urlSource.setUri("artc://<streaming URL>"); mAliPlayer.setDataSource(urlSource);NoteFor more information about how to set the playback source, see Basic Features.
Configure playback parameters.
You can configure playback parameters to improve the performance of RTS.
NoteApsaraVideo Player SDK V6.3.0 and later supports automatic low-latency optimization. If the playback URL starts with "artc://" and you have not manually modified the
mMaxDelayTime,mHighBufferDuration, ormStartBufferDurationvalues inPlayerConfig, the SDK automatically sets these parameters to 1000, 10, and 10 respectively during playback.To customize these settings, refer to the following example:
// 1. Get and modify the configuration. PlayerConfig config = mAliPlayer.getConfig(); if (playUrl.startsWith("artc://")) { // The maximum delay is 1000 ms. config.mMaxDelayTime = 1000; // The buffer duration for playback startup 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);Start playback.
// Prepare for playback. mAliPlayer.prepare(); // Start playback. mAliPlayer.start();
Control playback
The ApsaraVideo Player SDK for Android supports operations such as starting, stopping, and seeking to a specific time.
Start playback.
This operation starts video playback and is implemented by the
startAPI. The following example shows how to use this API.mAliPlayer.start();Stop playback.
This operation stops video playback and is implemented by the
stopAPI. The following example shows how to use this API.mAliPlayer.stop();Destroy the player.
You can destroy the player synchronously or asynchronously. The following is sample code:
// Synchronously destroys the player. The system automatically calls the stop method. mAliPlayer.release(); // Asynchronously destroys the player. The system automatically calls the stop method. mAliPlayer.releaseAsync();NoteA call to the synchronous destroy API returns a response only after the player resources are fully released. If a fast interface response is required, use the asynchronous destroy API and take note of the following:
Do not perform any other operations on the player object during the asynchronous destroy process.
Do not manually stop the player before you call the asynchronous destroy API. This is because the process already includes an asynchronous stop procedure.
Accessibility
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);RTS stream degradation.
NoteDegradation for playback failure applies only to timeout scenarios. If a playback timeout occurs and a degradation plan is configured, the player attempts to play the degraded stream. Otherwise, the player's onError callback is triggered. Exceptions such as 404, 403, or a streamer disconnection do not trigger degradation.
Automatic RTS degradation uses the same domain name to degrade from RTS to FLV. If your RTS and FLV domain names are different, you must configure a custom RTS degradation plan to specify the target FLV domain name.
Automatic RTS degradation (enabled by default)
When you play a stream from an RTS URL, if custom RTS degradation is not configured and RTS stream pulling fails, the player automatically degrades to the corresponding default FLV URL for playback. The following example shows how to do this.
// 1 indicates enabled, and 0 indicates disabled. It is enabled by default. AliPlayerGlobalSettings.setOption(AliPlayerGlobalSettings.ALLOW_RTS_DEGRADE, 1);Custom RTS degradation
When you play a stream from an RTS URL, you can set a degradation URL, such as an HLS or FLV URL. If RTS stream pulling fails, the player automatically degrades to the specified URL for playback. The following example shows how to do this.
PlayerConfig config = mAliPlayer.getConfig(); // Optional. Configure other settings. UrlSource urlSource = new UrlSource(); urlSource.setUri(downgradeUrl); // Specify the degraded streaming URL. mAliPlayer.enableDowngrade(urlSource, config);
Obtain the TraceID.
Each RTS playback session has a traceId that you can use for troubleshooting. You can obtain the traceId through the player event callback.
// Listen for the onInfo callback of the player and parse the DemuxerTraceID message. mAliPlayer.setOnInfoListener(infoBean -> { if (infoBean.getCode() == InfoCode.DemuxerTraceID) { String traceId = infoBean.getExtraMsg(); });