This topic describes how to get started with video playback using the ApsaraVideo Player SDK for Android.
Prerequisites
You have completed the SDK integration for the ApsaraVideo Player for Android.
Open-source sample project
For detailed code examples, see the BasicPlayback module in API-Example. This sample project is a Java-based Android project for the ApsaraVideo Player SDK that helps you quickly integrate the core features of the SDK.
Procedure
Step 1: Create the player
Create an AliPlayer instance using the AliPlayerFactory class.
// 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 the ApsaraVideo Player SDK, this feature performs end-to-end tracing of abnormal playback for a specific user or session. This helps you quickly diagnose issues and improve playback experience administration.
// Define the traceId value. It must be a unique identifier for your user or device, such as your business's user ID, or a device ID like an International Mobile Equipment Identity (IMEI) or identifier for advertisers (IDFA).
mAliPlayer.setTraceId("traceId");The player's playback quality monitoring, Tracing Analysis, and video playback statistics features depend on instrumentation log reporting. Playback quality monitoring provides data on overall playback quality. Tracing Analysis locates specific users or devices, analyzes their playback behavior, and helps you quickly identify issues such as playback errors.
When you create the player, the available features depend on the configuration of the setTraceId parameter:
If you do not set the
setTraceIdparameter (default): Instrumentation log reporting is enabled. You can then use the playback quality monitoring and video playback statistics features. The Tracing Analysis feature is unavailable.If you set the
setTraceIdparameter to a traceId: The traceId value is user-defined and must be a unique identifier for your user or device, such as your business's user ID or a device ID such as International Mobile Equipment Identity (IMEI) or identifier for advertisers (IDFA). After you set the traceId, instrumentation log reporting is enabled. You can then use the playback quality monitoring, Tracing Analysis, and video playback statistics features.If you set the
setTraceIdparameter toDisableAnalytics: Instrumentation log reporting is disabled. The playback quality monitoring, Tracing Analysis, and video playback statistics features are unavailable.
Step 2: Set the display view
The player supports three types of views: AliDisplayView, SurfaceView, and TextureView.
AliDisplayView: A unified view component encapsulated by the SDK. It provides standardized interfaces and is ready to use. This view is suitable for quick integrations and simple UI scenarios.
SurfaceView / TextureView: Native views. The application layer can fully manage the view lifecycle and rendering logic. These views are suitable for complex view hierarchies or scenarios that require fine-grained control and greater flexibility.
Declare the view in the XML file.
AliDisplayView
<!-- 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
Call the player's
setDisplayViewmethod to attach the 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 player's
setSurfacemethod 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 player's
setSurfacemethod 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) { } });
Step 3: Set the playback source
VidAuth playback (Recommended)
VidAuth vidAuth = new VidAuth();
vidAuth.setVid("Your Vid");// Required. The video ID (VideoId).
vidAuth.setPlayAuth("<yourPlayAuth>");// Required. The playback credential. Call the GetVideoPlayAuth operation of ApsaraVideo VOD to generate the credential.
vidAuth.setRegion("The region ID");// For ApsaraVideo Player SDK V5.5.5.0 and later, this parameter is deprecated. You do not need to set the region because the player automatically parses it. For versions earlier than V5.5.5.0, this parameter is required. It specifies the region where ApsaraVideo VOD is activated. The default value is cn-shanghai.
// vidAuth.setAuthTimeout(3600); // The validity period of the playback URL, in seconds. This value overwrites the validity period for URL signing that is set in the ApsaraVideo VOD console. If you do not pass this parameter, the default value 3600 is used. If you set this parameter, make sure that the period is longer than the video duration to prevent the playback URL from expiring before playback is complete.
mAliPlayer.setDataSource(vidAuth);VidSts playback
VidSts vidSts = new VidSts();
vidSts.setVid("Your Vid");// Required. The video ID (VideoId).
vidSts.setAccessKeyId("<yourAccessKeyId>");// Required. The AccessKey ID of the temporary AccessKey pair. Call the AssumeRole operation of Security Token Service (STS) to generate it.
vidSts.setAccessKeySecret("<yourAccessKeySecret>");// Required. The AccessKey secret of the temporary AccessKey pair. Call the AssumeRole operation of STS to generate it.
vidSts.setSecurityToken("<yourSecurityToken>");// Required. The STS token. Call the AssumeRole operation of STS to generate it.
vidSts.setRegion("The region ID");// Required. The region where ApsaraVideo VOD is activated. The default value is cn-shanghai.
mAliPlayer.setDataSource(vidSts);UrlSource playback
// Create a playback source object and set the playback URL.
UrlSource urlSource = new UrlSource();
// Required. The playback URL. It can be a third-party video-on-demand (VOD) URL, a playback URL from ApsaraVideo VOD, or a local video URL.
urlSource.setUri("The playback URL");
mAliPlayer.setDataSource(urlSource);For more information about how to set the playback source, see Basic Features.
Step 4: Start playback
// Prepare for playback.
mAliPlayer.prepare();
// Start playback.
mAliPlayer.start();Step 5: End playback
// Stop playback.
mAliPlayer.stop();
// Destroy the player.
mAliPlayer.release();
// Clear the reference to avoid a memory leak.
mAliPlayer = null;References
For more information about basic player features, such as playback control and event listeners, see Basic Features.