This topic describes how to integrate Player SDK for Web in your web application and complete basic video playback configuration.
Usage notes
In this topic, Player SDK for Web V2.25.1 is used as an example. We recommend that you use the latest version. For more information about the latest version of Player SDK for Web, see Download URL.
Player SDK for Web V2.14.0 or later supports playback of H.265 videos and Player SDK for Web V2.20.2 or later supports playback of H.266 videos. This topic describes how to configure Player SDK for Web to play H.264 videos. For more information about how to configure Player SDK for Web to play H.265 and H.266 videos, see Play H.265 and H.266 videos.
Preparations
You have obtained the domain name and license key bound to your website. For more information, see Manage license.
Quick integration of ApsaraVideo Player for Web
The following steps demonstrate how to integrate and configure Player SDK for Web in a simple web project. If you use a frontend framework such as React or Vue, you can obtain the source code demo and refer to the sample code for integration. For more information, see Online trial and source code of demos.
1. Import Player SDK for Web
Import by using NPM package
Add the
aliyun-aliplayer
dependency to your project.npm install aliyun-aliplayer --save
Import 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 ApsaraVideo Player SDK for Web V2.16.3 or later.
The old paths:
CSS: https://g.alicdn.com/de/prismplayer/version/skins/default/aliplayer-min.css
JS: https://g.alicdn.com/de/prismplayer/version/aliplayer-min.js
The new paths:
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, Player SDK for Web 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 with the desired version number.-->
<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
Player SDK for Web finds the DOM node with id="J_prismPlayer"
and renders the player UI on the node.
var player = new Aliplayer({
id: 'J_prismPlayer',
license: {
// Use the domain name and license key obtained in the preparations
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
}
});
Playback configuration
The following section shows parameter configurations for the player in different scenarios.
Play on-demand videos
Playback based on URLs
To play videos from URLs, you must set the source parameter to the playback URL. You can specify the playback URL of an audio or video file stored in a third-party VOD service or in ApsaraVideo VOD.
You can call the GetPlayInfo operation to obtain the playback URL of a media file stored in ApsaraVideo VOD. We recommend that you integrate ApsaraVideo VOD SDK to obtain media playback URLs. This frees you from complex signature calculations. For more information about the GetPlayInfo operation, visit OpenAPI Explorer.
var player = new Aliplayer(
{
id: "J_prismPlayer",
source: "<your play URL>", // The playback URL of an audio or video 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
To play videos based on VID and PlayAuth, you must set the vid parameter to the audio or video ID and set the playauth parameter to the playback credential.
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 to obtain the ID.
You can call the GetVideoPlayAuth operation to obtain the playback credential. We recommend that you integrate ApsaraVideo VOD SDK to obtain credentials for media playback. This frees you from complex signature calculations. For more information about the GetVideoPlayAuth operation, visit OpenAPI Explorer.
We recommend that you use this playback method. Compared with STS-based playback, VidAuth-based playback is easier to use and more secure. For more information about the comparison between the two playback methods, see Comparison between credentials and STS.
var player = new Aliplayer(
{
id: "J_prismPlayer",
width: "100%",
vid: "<your video ID>", // Required. The ID of the audio or video 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 STS
If you use STS-based playback, a temporary STS token rather than a playback credential is used. You need to call the AssumeRole operation to obtain an STS token in advance. 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. For more information about encrypted playback, see Play an encrypted video in a web browser.
For more information about how to initialize ApsaraVideo Player for Web, see Parameters.
Play live streams
URL-based live streaming
To stream content from URLs, you must set the source parameter to the streaming URL and the isLive parameter to true.
You can specify the streaming URL of a live stream in a third-party live streaming service or in ApsaraVideo Live. The ApsaraVideo Live console provides a URL generator to help you generate streaming URLs. For more information, see URL generator.
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 more information about DRM-encrypted live streaming, see Play an encrypted video.
Real-Time Streaming (RTS)
RTS allows you to play videos from URLs without the need to configure additional parameters.
You can use the URL generator in the ApsaraVideo Live console to generate an RTS URL. For more information, see URL generator.
ApsaraVideo Player implements RTS playback by integrating the RTS SDK. By default, the latest version of the RTS SDK is integrated. You can also specify the RTS SDK version by configuring the parameter. For example, specify rtsVersion: '2.2.1'.
If your browser does not support RTS or stream pulling over RTS fails, the system automatically plays the stream over a degraded protocol such as HTTP Live Streaming (HLS) or HTTP Flash Video (HTTP-FLV). If your browser supports FLV, the system plays the stream over FLV first.
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);
});
Related reading
For more information about player object properties, methods, and event configurations, see API operations.