This topic covers the advanced features of ApsaraVideo Player SDK for HarmonyOS NEXT and provides sample code.
Premium feature verification
Some player features require a premium license. For more information, see Player SDK Feature Details. To use these features, obtain the license as described in Obtain Player SDK License.
Set the listener at application startup or before calling any player API:
import { PrivateService } from 'premierlibrary';
let premiumVerifyCallback:OnPremiumLicenseVerifyCallback = {
onPremiumLicenseVerifyCallback: (type: PremiumBizType, isValid: boolean, errorMsg: string) => {
console.log("onPremiumLicenseVerifyCallback type: ", type, " isValid: ", isValid, " errorMsg: ", errorMsg);
}
}
PrivateService.setOnPremiumLicenseVerifyCallback(premiumVerifyCallback)In this code, PremiumBizType is the enum for premium features. When you use a related feature, the player verifies the license and returns the result via this callback. If isValid is false, errorMsg contains the reason.
Playback
List playback
For typical short video feed scenarios, ApsaraVideo Player SDK for HarmonyOS NEXT provides a comprehensive list playback feature. Using features like preloading, it significantly improves the startup speed of short videos. This feature is not recommended for long video scenarios.
Transparent video playback
Overview
ApsaraVideo Player SDK for HarmonyOS NEXT supports rendering the alpha channel to play dynamic effects such as transparent gifts. In scenarios like live streaming rooms, you can play transparent gift animations without obscuring the main content, which significantly enhances the user experience.
Limits
Transparent rendering is supported in ApsaraVideo Player SDK for HarmonyOS NEXT v6.21.0 and later.
Benefits
Using MP4 videos that contain transparency information for gift effects offers better animation quality, smaller file sizes, higher compatibility, and greater development efficiency.
Better animation quality: MP4 videos can preserve the original animation quality, including details and colors. Compared to other formats like APNG or IXD, MP4 can more accurately restore the animation effects created by designers.
Smaller file sizes: MP4 video files can be compressed more effectively than other formats like APNG or IXD, which improves loading speed and reduces network bandwidth consumption.
Higher compatibility: MP4 is a universal video format widely supported across various devices and browsers, allowing gift effects to be played and viewed on mainstream devices.
Using MP4 videos for gift effects is technically straightforward. It eliminates the need for complex parsing and rendering logic, allowing developers to focus on other features and improve development efficiency.
External subtitles
ApsaraVideo Player SDK for HarmonyOS NEXT allows you to add and switch external subtitles. Subtitle files in the SubRip Subtitle (SRT), SubStation Alpha (SSA), Advanced SubStation Alpha (ASS), and WebVTT (VTT) formats are supported. However, the SDK does not provide a built-in renderer for external subtitles.
This feature is available only in the Professional Edition. To use this feature, obtain a license for the Professional Edition. For more information, see Obtain a License for ApsaraVideo Player SDK.
Sample code:
Set subtitle-related listeners.
Add subtitles.
mAliPlayer.addExtSubtitle(url); // Add the subtitle after the player's onPrepared callback is triggered.Show or hide subtitles.
After you receive the
onSubtitleExtAddedcallback, use the following method to show or hide subtitles:// trackIndex: the subtitle track index. true: shows the specified track; false: hides the specified track. mAliPlayer.selectExtSubtitle(trackIndex, true);
Audio-only playback
You can enable audio-only playback by disabling video playback. Configure PlayerConfig before you call the prepare method.
let config:PlayerConfig | undefined = player.getConfig();
if (config) {
config.mDisableVideo = true; // Enable audio-only playback.
player.setConfig(config);
}Switching decoding modes
The decoding method must be switched before playback starts, as changes do not take effect during playback.
ApsaraVideo Player SDK for HarmonyOS NEXT provides hardware decoding capabilities for H.264 and H.265, which can be controlled by using the enableHardwareDecoder switch. Hardware decoding is enabled by default. If hardware decoding fails to initialize, the player automatically falls back to software decoding to ensure normal video playback. Sample code:
// Enable hardware decoding. This is enabled by default.
mAliyunVodPlayer.enableHardwareDecoder(true);If the player automatically switches from hardware to software decoding, the onInfo callback is triggered. Sample code:
private mOnVideoInfo: OnInfoListener = {
onInfo: (bean: InfoBean) => {
if (bean.getCode() === InfoCode.SwitchToSoftwareVideoDecoder) {
console.log("[HOS DEMO] [DOWNGRADE TO SOFTWARE DECODE]")
// Switched to software decoding.
}
}
}Adaptive bitrate streaming
The adaptive bitrate streaming (ABR) feature is available in Basic and high-performance Professional editions. Without the Professional Edition of ApsaraVideo Player SDK, its functionality is limited to the Basic Edition. To use the advanced ABR capabilities of the Professional Edition, refer to Get Player SDK License to obtain the required license.
HLS adaptive bitrate video streams can be generated by using a video packaging and transcoding template group in ApsaraVideo VOD. For more information, see Configure adaptive bitrate streaming for ApsaraVideo VOD.
For adaptive streams generated by ApsaraVideo VOD transcoding, if you use Vid-based playback, you must specify the default playback definition list as
DEFINITION_AUTOto obtain and play the adaptive stream. Otherwise, the player defaults to a low-definition stream for playback. For information about the default definition playback order, see If a video is transcoded into multiple definitions, which definition does the player SDK play by default? The following example shows how to specify the definition list for VidAuth-based playback:const vidAuthSource: VidAuth = new VidAuth(); let list: Definition[] = []; list.push(Definition.DEFINITION_AUTO); vidAuthSource.setDefinition(list); player.setVidAuthDataSource(vidAuthSource);
ApsaraVideo Player SDK for HarmonyOS NEXT supports adaptive bitrate streaming for HLS and DASH video streams. After the prepare method succeeds, you can call getMediaInfo to obtain information about each stream in the form of TrackInfo objects. Sample code:
let mediaInfo: MediaInfo | null | undefined = player?.getMediaInfo();
let trackInfos: TrackInfo[] | undefined = mediaInfo?.getTrackInfos();During playback, you can call the player's selectTrack method to switch the playback stream. Setting the value to AUTO_SELECT_INDEX enables adaptive bitrate streaming. Sample code:
let index: number | undefined = trackInfo?.getIndex();
if (index ) {
player?.selectTrack(index);
}
player?.selectTrack(TrackInfo.AUTO_SELECT_INDEX);The result of the switch is returned in the OnTrackChangedListener callback. You must set the listener before you call selectTrack. Sample code:
private mOnTrackChange: OnTrackChangedListener = {
// The definition switch is successful.
onChangedSuccess: (trackInfo: TrackInfo): void => {
// Switch successful.
}
}Optional: Before you call the player's selectTrack method to enable adaptive bitrate streaming, you can configure an upper definition limit for ABR switching. This prevents the player from automatically switching to a stream that exceeds the desired quality. We recommend that you call the following code before the player calls the prepare method, or before the list player calls the moveTo method, for it to take effect. Sample code:
let config:PlayerConfig | undefined = player.getConfig();
if (config) {
config.mMaxAllowedAbrVideoPixelNumber = 921600; // Set the ABR upper limit to 921600 pixels (width × height = 1280 × 720). ABR can switch only to streams with a pixel count less than or equal to this value.
player.setConfig(config);
}Snapshots
ApsaraVideo Player SDK for HarmonyOS NEXT provides a snapshot method to capture the current video frame. It captures the raw data and returns it as an ArrayBuffer. The callback interface is OnSnapShotListener. Sample code:
Preview
ApsaraVideo Player SDK for HarmonyOS NEXT, in conjunction with ApsaraVideo VOD configurations, supports the preview feature. It supports both VidSts and VidAuth (recommended for ApsaraVideo VOD) playback methods. For more information about how to configure and use the preview feature, see Preview videos.
After you configure the preview feature, use the VidPlayerConfigGen.setPreviewTime() method to set the preview duration for the player. The following example shows the VidSts playback method:
const vidSts: VidSts = new VidSts();
....
let configGen: VidPlayerConfigGen = new VidPlayerConfigGen();
configGen.setPreviewTime(20); // 20-second preview
vidSts.setPlayConfig(configGen); // Set for the playback source.
...When a preview duration is set, the server returns only the content for that period, not the full video.
VidPlayerConfigGen supports request parameters that are supported by the server. For more information, see Request parameters.
The preview feature is not supported for videos in FLV and MP3 formats.
Set Referer
ApsaraVideo Player SDK for HarmonyOS NEXT supports setting a Referer. In conjunction with the Referer-based allowlist and blocklist configured in the console, you can control access to your videos. Use the PlayerConfig object to set the request Referer. Sample code:
let config:PlayerConfig | undefined = player.getConfig();
if (config) {
// Set the Referer, for example, http://example.aliyundoc.com. Note: When you set a Referer, you must include the protocol.
config.mReferrer = referer;
player.setConfig(config);
}Set User-Agent
ApsaraVideo Player SDK for HarmonyOS NEXT provides PlayerConfig to set the request User-Agent (UA). After it is set, the player includes the UA information in requests. Sample code:
let config:PlayerConfig | undefined = player.getConfig();
if (config) {
// Set the UA.
config.mUserAgent = userAgent;
player.setConfig(config);
}Network timeout and retries
You can configure the network timeout and retry count for ApsaraVideo Player SDK for HarmonyOS NEXT by using the PlayerConfig object. Sample code:
let config:PlayerConfig | undefined = player.getConfig();
if (config) {
// Set the network timeout in milliseconds.
config.mNetworkTimeout = 5000;
// Set the number of retries after a timeout. The interval for each retry is networkTimeout. If networkRetryCount is set to 0, no retries are attempted, and the retry policy is determined by the app. The default value is 2.
config.mNetworkRetryCount=2;
player.setConfig(config);
}If a network issue causes the player to buffer, it retries up to NetworkRetryCount times. The interval between retries is mNetworkTimeout.
If the player remains in the loading state after multiple retries, the
onErrorevent is triggered. In this case, ErrorInfo.getCode() returns ErrorCode.ERROR_LOADING_TIMEOUT.If NetworkRetryCount is set to 0, the player triggers the
onInfoevent when a network retry times out. The InfoBean.getCode() of the event returns InfoCode.NetworkRetry. In this case, you can call the player'sreloadmethod to reload the network or perform other actions.
Buffer and latency control
ApsaraVideo Player SDK for HarmonyOS NEXT provides methods in PlayerConfig to control buffering and latency. Sample code:
HTTP headers
By using the PlayerConfig object, you can add custom HTTP header parameters to the player's requests. Sample code:
let config:PlayerConfig | undefined = player.getConfig();
if (config) {
// Define headers.
let headers: string[] = [];
headers.push("Host:example.com"); // For example, set the Host in the header.
// Set headers.
config.setCustomHeaders(headers);
player.setConfig(config);
}Video background color
ApsaraVideo Player SDK for HarmonyOS NEXT supports setting the background color for video rendering. The API and usage are described below.
API example
/**
* Sets the background color of the video.
*
* @param color ARGB
*
*/
setVideoBackgroundColor: (color: number) => void;Usage
// The parameter is an 8-digit hexadecimal value. The 8 digits are in pairs, representing A (alpha), R (red), G (green), and B (blue) in order.
// For example, 0x0000ff00 represents green.
player.setVideoBackgroundColor(0x0000ff00);Set playback domain for VidAuth
With VidAuth-based playback, you can specify fields such as the domain name corresponding to a vid. For more information about the supported fields, see GetPlayInfo request parameters. The API and usage are described below.
API example
/**
* Sets playback parameters.
*
* @param playConfig The playback parameters.
*/
setPlayerConfig: (playerConfig: VidPlayerConfigGen) => voidUsage
Use the addPlayerConfig method of the VidPlayerConfigGen interface to add the playDomain field.
const vidAuthSource: VidAuth = new VidAuth();
let configGen: VidPlayerConfigGen = new VidPlayerConfigGen();
// Add the playDomain field. For more information about the fields that can be added, see
// https://www.alibabacloud.com/help/en/apsaravideo-for-vod/latest/api-vod-2017-03-21-getplayinfo
configGen.addPlayerConfigString("playDomain", "com.xxx.xxx");
vidAuthSource.setPlayerConfig(configGen);
player.setVidAuthDataSource(vidAuthSource);Automatic source refresh
Automatic playback source refresh prevents interruptions caused by expired sources in authentication-based scenarios. When a source expires, the player triggers a listener to obtain a new URL, ensuring continuous playback.
Prerequisites
The Player SDK or All-in-One SDK is v7.9.0 or later.
You are using VidAuth-based playback or have configured URL authentication for your service.
VidAuth source
API example
/**
* Sets the listener for VidAuth source expiration events.
*
* This feature enables automated VidAuth source refresh to avoid playback interruptions
* caused by expiration. When the listener is triggered, you can refresh the VidAuth source
* and return the updated VidAuth using {@link SourceRefreshCallback#onSuccess}.
*
* @param listener The interface for listening to VidAuth source expiration events. See {@link OnVidAuthExpiredListener}.
*/
setOnVidAuthExpiredListener(listener: OnVidAuthExpiredListener): void;Components
Usage
You can obtain a playback credential by calling the GetVideoPlayAuth operation. We recommend integrating the ApsaraVideo VOD server-side SDK to obtain the credential, which simplifies the signing process. For more information, see OpenAPI Explorer.
private mOnVidAuthExpiredListener: OnVidAuthExpiredListener = ({
onVidAuthExpired(vidAuth: VidAuth, callback: SourceRefreshCallback<VidAuth> | null) {
let vid: String = vidAuth.getVid()
// ------------------- Start of user implementation -------------------
// Call your own function to get the new PlayAuth from your app server.
// This is an example. You must implement it according to your business logic.
XHR.fetch(``)
.then((data) => {
try {
console.log("Refresh - VidAuth", data.toString());
const response: IAuthResponse = JSON.parse(JSON.parse(data));
if (response && response.data && response.data.playAuth) {
// 1. Update the old vidAuth object with the new PlayAuth.
vidAuth.setPlayAuth(response.data.playAuth);
// 2. Pass the updated object back to the player by using the SDK's callback.
callback?.onSuccess(vidAuth);
} else {
// Pass the error message back to the player by using the SDK's callback.
callback?.onError('REFRESH_ERROR, invalid response data');
}
} catch (parseError) {
console.error("Parse or callback error:", parseError);
}
})
.catch((e: Error) => {
console.error("Network error:", e);
// Pass the error message back to the player by using the SDK's callback.
callback?.onError('REFRESH_ERROR, network fail');
})
}
})UrlSource
API example
/**
* Sets the listener for URL source expiration events.
*
* This feature enables URL refresh to avoid playback interruptions caused by
* URL expiration due to authentication. When the listener is triggered,
* you can refresh the URL source and return the updated URL source by using {@link SourceRefreshCallback#onSuccess}.
*
* @param listener Listener for handling URL source expiration events. See {@link OnURLSourceExpiredListener}.
*
* <p>For more information about how to configure URL authentication, see
* <a href="https://www.alibabacloud.com/help/en/apsaravideo-for-vod/latest/url-signing">URL authentication</a>.</p>
*/
setOnURLSourceExpiredListener(listener: OnURLSourceExpiredListener): void;Components
Usage
private mOnURLSourceExpiredListener: OnURLSourceExpiredListener = ({
onUrlSourceExpired(urlSource: UrlSource, callback: SourceRefreshCallback<UrlSource> | null) {
// 1. Extract the original URL from the expired URL (using authentication method A as an example).
let playURL: String = urlSource.getUri();
let urlAuthKeyIndex = playURL.indexOf('?auth_key=');
if (urlAuthKeyIndex == -1) {
urlAuthKeyIndex = playURL.indexOf("&auth_key=");
}
if (urlAuthKeyIndex === -1) {
callback?.onError('PARSE_ERROR, Cannot find auth_key in URL');
return;
}
// Restore the original resource address by removing the URL parameter part, for example, "?auth_key=".
// This ensures safe handling even if auth_key is not found.
const originalUrl = playURL.substring(0, urlAuthKeyIndex);
// 2. Generate a new authentication URL.
let key = '';
if (mAuthKey != null) {
key = mAuthKey
}
// Calculate the timeout for the playback URL (validity period).
const newExpireTime = Math.floor(Date.now() / 1000) + parseInt(mValidTime); // Current time + 1 hour
// Use the utility class to generate a new authentication URL.
const newAuthUrl = CdnAuthUtil.aAuth(originalUrl, key, newExpireTime);
// 3. Check the generated authentication URL and return the result through the callback.
if (newAuthUrl) {
let urlSource: UrlSource = new UrlSource();
urlSource.setUri(newAuthUrl);
callback?.onSuccess(urlSource);
} else {
callback?.onError('REFRESH_ERROR, refresh fail');
}
}
})Utility functions
Performance
Set player scene
Setting a player scene automatically applies optimal parameters for the selected scenario, such as buffer settings and feature toggles. This feature is compatible with custom parameters set via thesetConfig interface. Custom settings take precedence.
After you set a player scene, you can call the
getConfigmethod to view the parameter configuration.
API example
/**
* Sets a player scene.
* @param scene The specified player scene.
*/
setPlayerScene: (scene: PlayerScene) => void;Player scenes
export enum PlayerScene {
/**
* No scene
*/
NONE,
/**
* Long video scene: Suitable for videos longer than 30 minutes.
*/
LONG,
/**
* Medium video scene: Suitable for videos between 5 and 30 minutes.
*/
MEDIUM,
/**
* Short video scene: Suitable for videos up to 5 minutes.
*/
SHORT,
/**
* Live streaming scene
*/
LIVE,
/**
* Real-Time Streaming (RTS) live scene
*/
RTS_LIVE
}Usage
// Set the short video scene.
aliyunVodPlayer.setPlayerScene(PlayerScene.SHORT)
// Set the medium video scene.
aliyunVodPlayer.setPlayerScene(PlayerScene.MEDIUM)
// Set the long video scene.
aliyunVodPlayer.setPlayerScene(PlayerScene.LONG)
// Set the live streaming scene.
aliyunVodPlayer.setPlayerScene(PlayerScene.LIVE)Local cache
ApsaraVideo Player SDK for HarmonyOS NEXT provides a local cache feature. When playing a video multiple times, this feature improves startup speed and seek speed, reduces stuttering, and saves network traffic.
Enable local caching
The local cache feature is disabled by default. To use it, you must enable it manually by usingenableLocalCache inAliPlayerGlobalSettings. The following is a code sample:
If a video playback URL contains authentication parameters, these parameters can change, which reduces the local cache hit rate. To improve the cache hit rate for the same URL with different authentication parameters, you can remove the parameters from the URL before calculating its hash value (such as MD5) by using the
setCacheUrlHashCallbackinterface. For example, if a video playback URL with authentication parameters ishttp://****.mp4?aaa, you can usehttp://****.mp4to calculate the hash value when the video is loaded. However, for an encrypted M3U8 video, this approach can cause playback to fail. If you remove the authentication parameters from its keyURL before calculating the hash, different videos may be cached with the same key. Solution: In thesetCacheUrlHashCallbackcallback, you can check the domain name. Remove the authentication parameters only from the playback domain URL (such ashttp(s)://xxxxx.m3u8?aaaa), but do not remove them from the URL of the domain that corresponds to the keyURL (such ashttp(s)://yyyyy?bbbb).# playURL (Playback domain) curl "https://xxx/xxx-hd-encrypt-stream.m3u8?MtsHlsUriToken=xxx&auth_key=xxx" # Response content (m3u8 playlist) #EXTM3U #EXT-X-VERSION:3 #EXT-X-ALLOW-CACHE:YES #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:0 # keyURL (Key domain. Do not remove authentication parameters.) #EXT-X-KEY:METHOD=AES-128,URI="https://xxx/decrypt?Ciphertext=xxx&MtsHlsUriToken=xxx" #EXTINF:10.000000, xxx-hd-encrypt-stream-00001.ts?auth_key=xxx #EXTINF:10.000000, xxx-hd-encrypt-stream-00002.ts?auth_key=xxx ...If a server supports both HTTP and HTTPS for the same media file, you can improve the cache hit rate by normalizing the URL before calculating the hash. Examples:
If the playback URLs are
https://****.mp4andhttp://****.mp4, use****.mp4to calculate the hash.If the playback URL is
https://****.mp4, consistently convert it tohttp://****.mp4before calculating the hash.
If a playback URL for an HLS stream contains authentication parameters, you can set the
PlayerConfig.mEnableStrictAuthModefield to choose an authentication mode. The default value isfalsefor versions earlier than 7.13.0 andtruefor versions 7.13.0 and later.Non-strict authentication (
false): Authentication information is cached along with media data. If a video is only partially cached, the player uses the cached authentication to request subsequent segments. If the URL authentication has a short validity period, or if playback is resumed after a long pause, the authentication may expire. To ensure uninterrupted playback if authentication expires, you must integrate the automatic source refresh feature.Strict authentication (
true): Authentication information is not cached. The player authenticates every time playback starts. This can cause playback to fail without a network connection.
Enable or disable local caching for a single URL
You can enable or disable local caching for a specific URL by setting the appropriate property in thePlayerConfig object.
let config:PlayerConfig | undefined = player.getConfig();
if (config) {
// Specifies whether to enable local caching for the playback URL. Default value: true.
// For the local cache to be effective for this URL, global caching in AliPlayerGlobalSettings must also be enabled, and this value must be true.
// If set to false, local caching is disabled for this specific URL.
config.mEnableLocalCache = true;
player.setConfig(config);
}Preload
ApsaraVideo Player SDK for HarmonyOS NEXT provides a preload feature, which is an enhancement of the local cache. It improves video startup speed by caching a specified duration of content before playback begins.
This feature requires a Professional Edition license. For more information, see Obtain Player SDK License.
The preload feature has the following limits:
It supports only single media files, such as MP4, MP3, FLV, and HLS.
By default, the SDK automatically schedules network resources during preloading to minimize impact on the currently playing video. This strategy allows preload requests only after the current video's buffer reaches a certain threshold.
AliPlayerGlobalSettings.enableNetworkBalance(false);Enable the local cache feature. For details, see Local Cache.
Call the pre-initialization method.
privateService.preInitService(getContext());Set the data source.
VidAuth (recommended)
let vidAuth: VidAuth = new VidAuth(); vidAuth.setVid("Your_Vid");// Required. The Video ID. vidAuth.setPlayAuth("<yourPlayAuth>");// Required. The playback credential. Generate it by calling the GetVideoPlayAuth operation of the VOD service. vidAuth.setRegion("Your_Region");// Optional. Default value: cn-shanghai. vidAuth.setQuality("Selected_Quality");// "AUTO" represents auto bitrate.VidSts
let vidSts: VidSts = new VidSts(); vidSts.setVid("Your_Vid");// Required. The Video ID. vidSts.setAccessKeyId("<yourAccessKeyId>");// Required. The Access Key ID of the temporary STS token pair. Generate it by calling the AssumeRole operation of STS. vidSts.setAccessKeySecret("<yourAccessKeySecret>");// Required. The Access Key Secret of the temporary STS token pair. Generate it by calling the AssumeRole operation of STS. vidSts.setSecurityToken("<yourSecurityToken>");// Required. The Security Token of the temporary STS token pair. Generate it by calling the AssumeRole operation of STS. vidSts.setRegion("Your_Region"); // Optional. Default value: cn-shanghai. vidSts.setQuality("Selected_Quality");// "AUTO" represents auto bitrate.UrlSource
// Create a data source object and set the playback URL. let urlSource: UrlSource = new UrlSource(); // Required parameter. urlSource.setUri("Your_Playback_URL");Set the task parameters.
NoteThis applies only to multi-bitrate video. You only need to choose one of
setDefaultBandWidth,setDefaultResolution, orsetDefaultQuality.let mPreloadConfig: PreloadConfig = new PreloadConfig(); // Set the preload bitrate for multi-bitrate streams. preloadConfig.setDefaultBandWidth(400000); // Set the preload resolution for multi-bitrate streams. preloadConfig.setDefaultResolution(640 * 480); // Set the preload quality for multi-bitrate streams. preloadConfig.setDefaultQuality(“FD“); // Set the preload duration. preloadConfig.setDuration(1000);Add a task listener.
private PreloadListenerImpl: OnPreloadListener = { onError: (taskId: string, urlOrVid: string, errorInfo: ErrorInfo): void => { console.error("OnPreloadListener -> " + " taskId: " + taskId + " ErrorMsg: " + errorInfo.getMsg()) }, onCompleted: (taskId: string, urlOrVid: string): void => { console.info("OnPreloadListener -> " + "onCompleted" + " taskId: " + taskId); }, onCanceled: (taskId: string, urlOrVid: string): void => { console.info("OnPreloadListener -> " + "onCanceled" + " taskId: " + taskId) } }Build the task, add it to the
MediaLoaderV2instance, and start preloading.VidAuth (recommended)
// Build the preload task. let mPreloadTask: PreloadTask = new PreloadTask(vidAuth, mPreloadConfig); // Get the MediaLoaderV2 instance. let mediaLoaderV2: MediaLoaderV2 = MediaLoaderV2.getInstance(); // Add the task and start preloading. let taskId: String = mediaLoaderV2.addTask(mPreloadTask, PreloadListenerImpl)VidSts
// Build the preload task. let mPreloadTask: PreloadTask = new PreloadTask(vidSts, mPreloadConfig); // Get the MediaLoaderV2 instance. let mediaLoaderV2: MediaLoaderV2 = MediaLoaderV2.getInstance(); // Add the task and start preloading. let taskId: String = mediaLoaderV2.addTask(mPreloadTask, PreloadListenerImpl)UrlSource
// Build the preload task. let mPreloadTask: PreloadTask = new PreloadTask(urlSource, mPreloadConfig); // Get the MediaLoaderV2 instance. let mediaLoaderV2: MediaLoaderV2 = MediaLoaderV2.getInstance(); // Add the task and start preloading. let taskId: String = mediaLoaderV2.addTask(mPreloadTask, PreloadListenerImpl)Optional: Manage tasks.
mediaLoaderV2.cancelTask(taskId);// Cancels the preload task with the specified task ID. mediaLoaderV2.pauseTask(taskId);// Pauses the preload task with the specified task ID. mediaLoaderV2.resumeTask(taskId);// Resumes the preload task with the specified task ID.Delete the loaded files.
To save space, you can delete preloaded files. The SDK does not provide a deletion API; you must delete the files from the loading directory in your app.
Get the download speed
You can get the download speed of the currently playing video from the onInfo callback using thegetExtraValue method. The following is a code sample:
private mOnVideoInfo: OnInfoListener = {
// Information from the player, such as the current progress and buffer position.
onInfo: (bean: InfoBean) => {
if (bean.getCode() === InfoCode.CurrentDownloadSpeed) {
/// Current download speed
let extraValue: number = bean.getExtraValue();
}
}
}Network features
HTTPDNS
HTTPDNS sends DNS resolution requests to a dedicated HTTPDNS server for faster, more stable results and to reduce the risk of DNS hijacking.
Alibaba Cloud Player SDK offers an enhanced HTTPDNS feature, designed specifically for Alibaba Cloud CDN domain names. It supports precise scheduling on the Alibaba Cloud CDN network and real-time resolution updates, effectively improving network performance.
The enhanced HTTPDNS feature is supported in Alibaba Cloud Player SDK for HarmonyOS NEXT v6.21.0 and later. To enable this feature, you must first submit a ticket or contact your Alibaba Cloud account manager.
Enhanced HTTPDNS example
The HTTPDNS feature serves only Alibaba Cloud CDN domain names. Ensure that the accelerated domain name is configured for Alibaba Cloud CDN and is running properly. To add and configure an accelerated domain name in ApsaraVideo for VOD, see Add an accelerated domain name. For more information about CDN domain names, see Alibaba Cloud CDN.
// Enable enhanced HTTPDNS.
AliPlayerGlobalSettings.enableEnhancedHttpDns(true);
// Optional: Add a domain name for HTTPDNS pre-resolution.
DomainProcessor.addPreResolveDomain("player.***alicdn.com");HTTP/2
Alibaba Cloud Player SDK for HarmonyOS NEXT supports HTTP/2. This protocol improves playback performance by using multiplexing to prevent head-of-line (HOL) blocking. Sample code:
AliPlayerGlobalSettings.setUseHttp2(true);Video download
The ApsaraVideo Player SDK for HarmonyOS NEXT lets you download videos from ApsaraVideo VOD and cache them for offline playback. It offers two download methods: normal download and secure download.
Normal download
Alibaba Cloud does not encrypt videos downloaded in this mode, so they can be played by third-party players.
Secure download
Alibaba Cloud encrypts videos downloaded in this mode. Only ApsaraVideo Player can play these videos; third-party players cannot.
Usage notes
The video download feature is supported only for VidSts and VidAuth sources.
To use the video download feature, you must enable and configure the download mode in the ApsaraVideo VOD console. For more information, see offline download.
Resumable downloads are supported.
Procedure
Optional: Configure the key file. This step is required only for secure download.
NoteEnsure the configured key file matches your app information to prevent download failures.
For secure downloads, you must configure the player SDK with the key file generated in the ApsaraVideo VOD console. This file is required for decryption and verification during video download and playback. To generate the key file, see Enable secure download.
After generating the key file, place it in the
rawfiledirectory of your project (create the directory if it does not exist). The directory structure is as follows:src/main/resources/ └── rawfile/ └── encrypt/ └── encryptedApp.datAfter placing the file, configure it using the following sample code:
try { getContext().resourceManager.getRawFileContent('encrypt/encryptedApp.dat', (error, value) => { if (error != null) { console.log('downloader init error is ' + error); } else { let rawFile = value; let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM : true }); let rawFileString = textDecoder.decodeToString( rawFile , {stream: false}); console.log(`downloader get encryptedApp.dat value: ${rawFileString}`); PrivateService.initServiceWithBytes(getContext(), rawFileString); } }) } catch (error) { let code = (error as BusinessError).code; let message = (error as BusinessError).message; console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`); }Create and set up the downloader.
Create a downloader using AliDownloaderFactory. Sample code:
...... // Create the downloader. let mAliDownloader:AliMediaDownloader = AliDownloaderFactory.create(getApplicationContext()); // Configure the path to save downloaded files. mAliDownloader.setSaveDir(getContext().cacheDir);Set listeners.
You can set multiple listeners for the downloader. Sample code:
Prepare the download source.
Use the
preparemethod to prepare the download source. VidSts and VidAuth sources are supported. Sample code:VidSts
// Create a VidSts source. let aliyunVidSts : VidSts = new VidSts(); aliyunVidSts.setVid("Your Video ID");// The Video ID. aliyunVidSts.setAccessKeyId("<yourAccessKeyId>");// The AccessKey ID of the temporary STS token, obtained by calling the STS AssumeRole operation. aliyunVidSts.setAccessKeySecret"<yourAccessKeySecret>");// The AccessKey Secret of the temporary STS token, obtained by calling the STS AssumeRole operation. aliyunVidSts.setSecurityToken("<yourSecurityToken>");// The security token, obtained by calling the STS AssumeRole operation. aliyunVidSts.setRegion("Your region");// The region where ApsaraVideo VOD is enabled. Default: cn-shanghai. // If you enabled HLS standard encryption parameter passthrough in the ApsaraVideo VOD console // and the default parameter name is MtsHlsUriToken, you need to set the config // and pass it to the VidSts object as shown in the following code. // If you have not enabled HLS standard encryption parameter passthrough, you can skip the following code. let config:VidPlayerConfigGen = new VidPlayerConfigGen(); config.setMtsHlsUriToken("<yourMtsHlsUriToken>"); aliyunVidSts.setPlayerConfig(config); // Prepare the download source. mAliDownloader.prepare(aliyunVidSts)VidAuth
// Create a VidAuth source. let vidAuth : VidAuth = new VidAuth(); vidAuth.setVid("Your Video ID");// The Video ID. vidAuth.setPlayAuth("<yourPlayAuth>");// The playback credential, obtained by calling the ApsaraVideo VOD GetVideoPlayAuth operation. vidAuth.setRegion("Your region");// Deprecated. The region is parsed automatically. // If you enabled HLS standard encryption parameter passthrough in the ApsaraVideo VOD console // and the default parameter name is MtsHlsUriToken, you need to set the config // and pass it to the VidAuth object as shown in the following code. // If you have not enabled HLS standard encryption parameter passthrough, you can skip the following code. let config:VidPlayerConfigGen = new VidPlayerConfigGen(); config.setMtsHlsUriToken("<yourMtsHlsUriToken>"); vidAuth.setPlayerConfig(config); // Prepare the download source. mAliDownloader.prepare(vidAuth);
NoteThe format of the downloaded file is the same as that of the source file and cannot be changed.
If you enable HLS standard encryption parameter passthrough in the ApsaraVideo VOD console, the default parameter name is
MtsHlsUriToken. For more information, see HLS standard encryption parameter passthrough. In this case, you must set theMtsHlsUriTokenvalue in the source object as shown in the preceding code.
Select a track and start the download.
When preparation is complete, the
OnPreparedListenercallback is triggered. The returnedTrackInfocontains information about each video stream, such as its definition. Select a track to download. Sample code:mAliDownloader.setOnPreparedListener({ onPrepared: (mediaInfo: MediaInfo) => { // The download source is successfully prepared. let trackInfos : TrackInfo[] = mediaInfo.getTrackInfos(); // For example, download the first track. mAliDownloader.selectItem(trackInfos[0].getIndex()); // Start the download. mAliDownloader.start(); } });(Optional) Update the download source.
To prevent VidSts and VidAuth from expiring, update the download source information before starting the download. For example:
// Update the download source. mAliDownloader.updateVidStsSource(VidSts); // Start the download. mAliDownloader.start();Release the downloader after the download completes or fails.
When the download finishes, call the
releasemethod within theonCompletionoronErrorcallback.mAliDownloader.stop(); mAliDownloader.release();Optional: Delete the downloaded files.
You can delete downloaded files during or after the download. Sample code:
// Delete the file using the object. mAliDownloader.deleteFile(); // Delete the file using the static method. If the deletion succeeds, 0 is returned. AliDownloaderFactory.deleteFile("Path to the download folder to be deleted","Video ID","Video format","Index of the downloaded video");
Next steps
To play the downloaded videos with ApsaraVideo Player, follow these steps:
After the download is complete, obtain the absolute path of the video file.
let path: string = mAliDownloader.getFilePath();Set the absolute path using a UrlSource to play the video.
let urlSource: UrlSource = new UrlSource(); urlSource.setUri(path);// Set the absolute path of the downloaded video file. aliPlayer.setUrlDataSource(urlSource);
Encrypted video playback
On-demand videos support HLS encryption and Alibaba Cloud proprietary encryption. For more information, see Play encrypted videos.