Integrate ApsaraVideo Player SDK for Web into your web application and configure basic video playback.
Usage notes
This topic uses ApsaraVideo Player SDK for Web V2.25.1 as an example. We recommend the latest version. To download it, see Download ApsaraVideo Player SDK.
ApsaraVideo Player SDK for Web V2.14.0+ supports H.265 video playback, and V2.20.2+ supports H.266. This topic covers H.264. For H.265 and H.266, see Play H.265 and H.266 videos.
Preparations
You have obtained the domain name and license key for your website. Manage license.
Quick access using AI Skill
If you use AI programming tools that support Agent Skills (such as Qoder and Claude Code), you can install the official Skill of the player, and the AI generates the integration code that can be directly run.
Installation.
Method 1: Execute the command to install automatically in the root directory of the project.
npx skills add https://player.alicdn.com/file/aliplayer-web-setup.tar.gzMethod 2: Manually download the decompression package and put it in the skills directory of the AI programming tool.
Copy the aliplayer-web-setup directory to the skills directory of the corresponding tool, for example:
GLOBAL EFFECTIVE:~/.qoder/skills/aliplayer-web-setup/
Effective for current item only:<Project Root Directory>/.qoder/skills/aliplayer-web-setup/
Please refer to the official documentation for the skills directory path of other AI programming tools.
NoteAfter the installation of the two methods is completed, the AI programming tool needs to be restarted to make the Skill take effect.
Use.
Describe the requirement in natural language, for example:Help me connect to Aliyun player in React project, play on-demand video, need private encryption。
The AI will first confirm the front-end framework, on-demand/live broadcast, whether to encrypt, video source mode and other information, and then output the complete player integration code.
The generated code uses the test License by default and is limited to local debugging. The official launch must be replaced by the official License applied in the console. For details, see Manage license.
Manual Quick Access
These steps integrate ApsaraVideo Player SDK for Web in a basic web project. For React or Vue, download the demo from Online trial and demo source code.
1. Import Web SDK
Import by using NPM package
Add the
aliyun-aliplayerdependency to your project.npm install aliyun-aliplayer --saveImport the code package and style file in your code.
import Aliplayer from 'aliyun-aliplayer'; import 'aliyun-aliplayer/build/skins/default/aliplayer-min.css';
Import by using script tag
The paths in which the JavaScript and CSS files are stored have changed for V2.16.3 and later.
Original:
CSS: https://g.alicdn.com/de/prismplayer/version/skins/default/aliplayer-min.css
JS: https://g.alicdn.com/de/prismplayer/version/aliplayer-min.js
New:
CSS: https://g.alicdn.com/apsara-media-box/imp-web-player/version/skins/default/aliplayer-min.css
JS: https://g.alicdn.com/apsara-media-box/imp-web-player/version/aliplayer-min.js
<!--In this topic, Web SDK V2.25.1 is used as an example. If you want to use other versions, obtain the version number and replace 2.25.1 in the sample code.-->
<head>
<link rel="stylesheet" href="https://g.alicdn.com/apsara-media-box/imp-web-player/2.25.1/skins/default/aliplayer-min.css" /> // Required. You must add this CSS file.
<script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/apsara-media-box/imp-web-player/2.25.1/aliplayer-min.js"></script> // Required. You must add the .js file.
</head>2. Provide a mount element
Add a <body> tag and a <div> node to mount the player UI. Sample code:
<body>
<div id="J_prismPlayer"></div>
</body>3. Initialize the SDK
The SDK locates the DOM node with id="J_prismPlayer" and renders the player UI.
var player = new Aliplayer({
id: 'J_prismPlayer',
license: {
// Use the domain name and license key prepared
domain: "example.com", // The domain name that you specified when you applied for a license
key: "example-key" // The license key displayed in the ApsaraVideo VOD console after you have applied for a license
}
});Play a video
Configure the player for different scenarios.
Play on-demand videos
Playback based on URLs
Set the source parameter to the playback URL of a media file from a third-party service or ApsaraVideo VOD.
Call the GetPlayInfo operation to obtain the playback URL of a media file in ApsaraVideo VOD. We recommend integrating ApsaraVideo VOD SDK to avoid complex signature calculations. Try the API in OpenAPI Explorer.
var player = new Aliplayer(
{
id: "J_prismPlayer",
source: "<your play URL>", // The playback URL of a media file stored in a third-party VOD service or in ApsaraVideo VOD.
},
function (player) {
console.log("The player is created.");
}
);Playback based on VID and PlayAuth
Set vid to the media ID and playauth to the playback credential.
After uploading a media file, find its ID in the ApsaraVideo VOD console under Media Files > Audio/Video. Alternatively, call the SearchMedia operation.
Call the GetVideoPlayAuth operation to obtain the playback credential. We recommend integrating ApsaraVideo VOD SDK to avoid complex signature calculations. Try the API in OpenAPI Explorer.
VidAuth-based playback is recommended over STS for simplicity and security. For comparison, see Credential method vs. STS method.
var player = new Aliplayer(
{
id: "J_prismPlayer",
width: "100%",
vid: "<your video ID>", // Required. The ID of the media file. Example: 1e067a2831b641db90d570b6480f****.
playauth: "<your PlayAuth>", // Required. The playback credential.
// authTimeout: 7200, // The validity period of the playback URL. Unit: seconds. This setting overwrites the validity period that you configured in the ApsaraVideo VOD console. If you leave this parameter empty, the default value 7200 is used. The validity period must be longer than the actual duration of the video. Otherwise, the playback URL expires before the playback is complete.
},
function (player) {
console.log("The player is created.");
}
);
Playback based on VID and STS
STS-based playback uses a temporary STS token instead of a playback credential. Call the AssumeRole operation to obtain an STS token. For more information, see Obtain an STS token.
var player = new Aliplayer(
{
id: "J_prismPlayer",
width: "100%",
vid: "<your video ID>", // Required. After you upload an audio or video file, you can log on to the ApsaraVideo VOD console and choose Media Files > Audio/Video to view the ID of the audio or video file. Alternatively, you can call the SearchMedia operation provided by the ApsaraVideo VOD SDK to obtain the ID. Example: 1e067a2831b641db90d570b6480f****.
accessKeyId: "<your AccessKey ID>", // Required. The AccessKey ID is returned when the temporary STS token is generated.
securityToken: "<your STS token>", // Required. The STS token. To obtain an STS token, call the AssumeRole operation.
accessKeySecret: "<your AccessKey Secret>", // Required. The AccessKey secret is returned when the temporary STS token is generated.
region: "<region of your video>", // Required. The ID of the region in which the media asset resides, such as cn-shanghai, eu-central-1, or ap-southeast-1.
// authTimeout: 7200, // The validity period of the playback URL. Unit: seconds. This setting overwrites the validity period that you configured in the ApsaraVideo VOD console. If you leave this parameter empty, the default value 7200 is used. The validity period must be longer than the actual duration of the video. Otherwise, the playback URL expires before the playback is complete.
},
function (player) {
console.log("The player is created.");
}
);Encrypted playback
ApsaraVideo VOD supports Alibaba Cloud proprietary cryptography and DRM encryption. Play an encrypted video in a web browser.
When the player is initialized by using the VID playback mode, the Aliplayer SDK sends a GetPlayInfo request to vod.{region}.aliyuncs.com(vod-product.{region}aliyuncs.com) to obtain the playback address. If the network cannot access the domain name (for example, an intranet environment), the request times out or fails. Ensure that vod.{region}.aliyuncs.com(vod-product.{region}aliyuncs.com) and the CDN domain name are accessible from the network.
For all initialization options, see Parameters.
Play live streams
URL-based live streaming
Set source to the streaming URL and isLive to true.
The streaming URL can be from a third-party service or ApsaraVideo Live. Generate URLs with the URL generator in the ApsaraVideo Live console.
var player = new Aliplayer(
{
id: "J_prismPlayer",
source: "<your play URL>", // The streaming URL can be a third-party streaming URL or a streaming URL that is generated in ApsaraVideo Live.
isLive: true, // Specifies whether to play live streams.
},
function (player) {
console.log("The player is created.");
}
);DRM-encrypted live streaming
For DRM-encrypted live streaming: Play an encrypted video.
Real-Time Streaming (RTS)
RTS plays videos from URLs without additional parameters.
Generate an RTS URL with the URL generator in the ApsaraVideo Live console.
The player integrates the RTS SDK for RTS playback. The latest version is used by default. Override with the rtsVersion parameter.
If RTS is unavailable, the player falls back to HLS or HTTP-FLV. FLV is prioritized when supported.
var player = new Aliplayer(
{
id: "J_prismPlayer",
source: "<your play URL>", // The RTS playback URL. The artc:// protocol is used.
isLive: true, // Specifies whether to play live streams.
// rtsFallback: false, //Optional. Specifies whether to enable the RTS playback degradation feature. Default value: true.
// rtsFallbackType: 'HLS', //Optional. The degraded protocol that you want to use. You can specify HLS or FLV. By default, this parameter is left empty. In this case, the default policy is used and the system tries to play the stream over FLV first. If your browser does not support FLV, the system plays the stream over HLS.
// rtsFallbackSource: '<your play URL>', // Optional. The degraded protocol that you want to use.
// rtsVersion: 'x.x.x', // Optional. The version of the RTS SDK.
},
function (player) {
console.log("The player is created.");
}
);
// The event that is triggered when a stream is pulled over RTS. Listen for this event to obtain the TraceId. In the event callback, traceId indicates the TraceId that is used for stream pulling and source indicates the playback URL of the RTS stream.
player.on("rtsTraceId", function (event) {
console.log("EVENT rtsTraceId", event.paramData);
});
// The event that is triggered when a degraded protocol is used for playback. reason indicates the degradation cause and fallbackUrl indicates the alternative URL.
player.on("rtsFallback", function (event) {
console.log(" EVENT rtsFallback", event.paramData);
});
Reference
For player properties, methods, and events, see Aliplayer API Reference.