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 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.
Integrate SDK
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 --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 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 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.
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);
});
Generic Media Management Service video playback
Player SDK for Web V2.10.0 or later is required.
A playback solution based on the mediaAuth method for videos in Generic Media Management Service.
Obtain mediaAuth from the Generic Media Management Service console under Media Management > Video Management > Manage > Basic Configuration.
var player = new Aliplayer(
{
id: "J_prismPlayer",
mediaAuth: "<your mediaAuth>", // The playback credential of the video, which can be obtained from the Generic Media Management Service console.
},
function (player) {
console.log("The player is created.");
}
);
// You can also use the replayByMediaAuth function to switch between different mediaAuth playbacks (provided that the player has been initialized with mediaAuth).
// player.replayByMediaAuth('another mediaAuth');
Reference
-
For player properties, methods, and events, see Aliplayer API Reference.