This topic describes how to implement RTS using the ApsaraVideo Player SDK on Android.
Integrate the SDK
Add the ApsaraVideo Player SDK dependency files as follows:
Maven integration
Add the Maven repository address.
Add the Maven repository address in the build.gradle file of the root directory.
// Maven repository address for Alibaba Cloud related 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 in your project.
def player_sdk_version = "x.x.x" // We recommend using the latest version. def rts_sdk_version = "7.12.0" // Independent version number, currently the latest version. // Player main library implementation "com.aliyun.sdk.android:AliyunPlayer:$player_sdk_version-full" // Bridge layer (AlivcArtc) between the player and RTS component. The version number must be consistent with the player, and it must be integrated with the RTS component. implementation "com.aliyun.sdk.android:AlivcArtc:$player_sdk_version" // RTS component implementation "com.aliyun.rts.android:RtsSDK:$rts_sdk_version"ImportantThe bridge layer (AlivcArtc) between the player and the RTS component must be consistent with the player's version number and must be integrated simultaneously with the RTS component.
For player versions, see SDK download. We recommend that you use the latest version. The minimum supported version is V5.4.5.0.
For FAQs, see Android Player FAQ.
Use ApsaraVideo Player SDK interfaces
Call the methods provided by the ApsaraVideo Player SDK to use the RTS feature. For information about other features of the ApsaraVideo Player SDK, see Advanced features and API reference.
The following is sample code. For detailed code, see the RTS Real-Time Streaming playback module in the API-Example project. This project is a Java-based ApsaraVideo Player SDK Android sample project that helps developers quickly master core SDK feature integration.
When implementing RTS stream pulling using the ApsaraVideo Player SDK, you cannot call the pause method to pause the live stream. Instead, call stop to stop playback, then call prepare to play again.
Seeking (dragging) is not supported.
Load the RTS library
Add System.loadLibrary("RtsSDK"); to the appropriate Activity as needed.
static {
System.loadLibrary("RtsSDK");
}Create a player
Create a player.
You can create an AliPlayer using the
AliPlayerFactoryclass.// Create a player instance AliPlayer mAliPlayer = AliPlayerFactory.createAliPlayer(context); // Pass in the traceId // Optional: We recommend using the 'Player single-point tracing' feature. When an exception occurs during video playback using ApsaraVideo Player SDK, use the single-point tracing feature to perform full-link tracing of abnormal playback behavior for a specific user or playback session. This helps you quickly diagnose the cause of the problem and effectively improve playback experience administration efficiency. // Define the traceId value yourself. It must be a unique identifier for your user or user device, such as your business's user ID, IMEI, or IDFA. mAliPlayer.setTraceId("traceId");NoteThe player provides the Playback Quality Monitoring feature (to view data about overall playback quality), the Single-Point Troubleshooting feature (to locate a specific user or device, analyze their playback behavior, and quickly identify playback abnormalities), and the Video Playback Statistics feature—all of which rely on the instrumentation log reporting feature.
When you create a player, the available features are determined by the setting of the
setTraceIdparameter, as follows:If the
setTraceIdparameter is not passed (default): The instrumentation log reporting feature is enabled. You can then use the playback quality monitoring and video playback statistics features, but the single-point tracing feature is not available.If you pass the
setTraceIdparameter with a trace ID, you define the trace ID value yourself. It must be a unique identifier for your user or user device, such as your business's user ID, IMEI, or IDFA. After you pass the trace ID, the instrumentation log reporting feature is enabled. You can then use playback quality monitoring, single-point tracing, and video playback statistics.If you set the
setTraceIdparameter toDisableAnalytics, instrumentation log reporting is disabled. You cannot use the playback quality monitoring, single-point tracing, or video playback statistics features.
Set the display view.
The player supports AliDisplayView (recommended), SurfaceView, and TextureView. Choose one as needed.
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" />Set the display view.
AliDisplayView (recommended)
Call the player's
setDisplayViewinterface to bind the player view.AliDisplayView mAliDisplayView = findViewById(R.id.display_view); // Set the playback view type using setPreferDisplayView() mAliDisplayView.setPreferDisplayView(AliDisplayView.DisplayViewType.SurfaceView); mAliPlayer.setDisplayView(mAliDisplayView);SurfaceView
Call the player's
setSurfaceinterface to bind 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 player's
setSurfaceinterface to bind 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) { } });
Set the playback source.
The player supports four playback sources: VidSts, VidAuth, VidMps, and UrlSource. UrlSource is for direct URL playback. Set the URL to the
artc://protocol to use the RTS service.UrlSource urlSource = new UrlSource(); urlSource.setUri("artc://<Streaming URL>"); mAliPlayer.setDataSource(urlSource);NoteFor more information about methods to set the playback source, see Basic Features.
Configure playback parameters.
Configure playback parameters to improve RTS performance.
NoteApsaraVideo Player SDK V6.3.0 and later support automatic optimal low-latency configuration. If the playback URL starts with "artc://" and you have not manually modified the
mMaxDelayTime,mHighBufferDuration, andmStartBufferDurationvalues inPlayerConfig, the SDK automatically uses 1000, 10, and 10, respectively, for actual playback.To customize control, refer to the following example:
// 1. Get and modify the configurations PlayerConfig config = mAliPlayer.getConfig(); if (playUrl.startWith("artc://")) { // Maximum delay: 1000 milliseconds config.mMaxDelayTime = 1000; // Buffer duration for playback startup: 10 milliseconds config.mStartBufferDuration = 10; // Buffer duration for stuttering recovery: 10 milliseconds config.mHighBufferDuration = 10; } else { // Use default configurations or customize to other configurations } // 2. Apply the configurations 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 playing from a specific point in time, starting, and stopping playback.
Start playback.
This refers to starting video playback, implemented by the
startinterface. For example:mAliPlayer.start();Stop playback.
This refers to stopping video playback, implemented by the
stopinterface. For example:mAliPlayer.stop();Destroy the player.
Destroy the player instance using either synchronous or asynchronous destruction methods, as follows:
// Synchronous destruction. The system automatically calls the stop interface. mAliPlayer.release(); // Asynchronous destruction. The system automatically calls the stop interface. mAliPlayer.releaseAsync();NoteThe synchronous destruction method returns only after player resources are fully released. If you have high requirements for UI response speed, use the asynchronous destruction method and note the following:
Avoid performing any other operations on the player object during asynchronous destruction.
You do not need to manually stop the player before calling the asynchronous destruction method, because the process internally includes an asynchronous stop flow.
Auxiliary Features
Log switch.
// 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 RTS degradation.
NotePlayback failure degradation applies only to timeout scenarios. If any of the following degradation schemes are configured in a playback timeout scenario, degraded playback processing occurs; otherwise, the player's onError callback is triggered. For example, exceptions such as 404, 403, or streamer disconnection do not trigger degradation.
RTS automatic degradation uses the same domain name to achieve RTS to FLV degradation. If your RTS and FLV domain names are different, configure the RTS custom degradation scheme to specify the target FLV domain name.
RTS automatic degradation (enabled by default)
In scenarios where Real-Time Streaming (RTS) addresses are used for playback, if RTS custom degradation is not configured and RTS stream pulling fails, the system automatically degrades to the default FLV address corresponding to RTS for playback. For example:
// 1 means enabled, 0 means disabled. Enabled by default. AliPlayerGlobalSettings.setOption(AliPlayerGlobalSettings.ALLOW_RTS_DEGRADE, 1);RTS custom degradation
In scenarios where Real-Time Streaming (RTS) addresses are used for playback, by setting the RTS degradation address (such as an HLS or FLV address), if RTS stream pulling fails, the system automatically degrades to that address for playback. For example:
PlayerConfig config = mAliPlayer.getConfig(); // Optional: Configure other items in config UrlSource urlSource = new UrlSource(); urlSource.setUri(downgradeUrl); // Set the degradation URL mAliPlayer.enableDowngrade(urlSource, config);
Obtain the TraceID.
Each low-latency playback has a traceId, which is used for troubleshooting. Obtain the traceId through the player 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(); });