This topic describes how to integrate and use ApsaraVideo Player SDK for Web to play audio and video files.
Prerequisites
ApsaraVideo Player SDK for Web allows you to play audio and video files by using the HTML5 player or Flash player. Before you integrate ApsaraVideo Player SDK for Web, you must determine the player that you want to use and learn about its features and compatibility with different browsers. For more information, see Overview.
Integrate the SDK
Playback of VODs
To play videos from URLs, you must set the source parameter to the playback URL. The playback URL can be 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 an audio or video file stored in ApsaraVideo VOD. We recommend that you integrate an ApsaraVideo VOD server SDK to obtain the playback URL of an audio or video file stored in ApsaraVideo VOD. This frees you from complex signature calculation. For more information about the GetPlayInfo operation, see OpenAPI Explorer.
Sample code for playback from URLs
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.')
});
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 audio or video ID. Alternatively, you can call the SearchMedia operation provided by a server SDK to obtain the audio or video ID. You can call the GetVideoPlayAuth operation to obtain the playback credential. We recommend that you integrate an ApsaraVideo VOD server SDK to obtain the playback credential. This frees you from complex signature calculation. For more information about the GetVideoPlayAuth operation, see OpenAPI Explorer.
We recommend that you use this playback method. Compared with STS-based playback, playback implemented based on PlayAuth is easier and more secure. For more information about the comparison between the two playback methods, see Comparison between credentials and STS.
Sample code for playback based on VID and PlayAuth
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.
},function(player){
console.log('The player is created.')
});
If you use STS-based playback, a temporary STS token rather than a playback credential is used. You must obtain the temporary STS token before you use this method to play videos. For more information, see Create a role and grant temporary access permissions to the role by using STS. You must set the SecurityToken parameter to the obtained STS token and set the AccessKeyId and AccessKeySecret parameters to the AccessKey ID and AccessKey secret that are generated based on the STS token.
Sample code for STS-based playback
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 audio or video ID. Alternatively, you can call the SearchMedia operation provided by a server SDK to obtain the audio or video ID. For more information, see SearchMedia. 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 temporary playback credential. The STS token must be generated in advance. For more information, see Create a role and grant temporary access permissions to the role by using STS.
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.
},function(player){
console.log('The player is created.')
});
You can use MPS to play media files if you are using the MPS service. You must obtain the following information before you use MPS for playback.
- Vid: the ID of the media asset in MPS.
- AcessKeyId and AccessKeySecret: the AccessKey pair that is issued together with the temporary STS token.
- SecurityToken: the temporary STS token.
- domainRegion: the region in which the media asset resides.
- authInfo: the authorization information.
For information about media assets in MPS, see Play videos.
Sample code for MPS playback
var player = new Aliplayer({
id: 'J_prismPlayer',
width: '100%',
vid : '<your media ID in MPS>',// Required. To query the ID of your media asset in the MPS console, you can log on to the MPS console and choose Media Management > Media List in the left-side navigation pane. On the page that appears, you can view the ID of the media asset. Example: 1e067a2831b641db90d570b6480f****.
accId: '<your AccessKey ID>',// Required. The AccessKey secret is returned when the temporary STS token is generated.
accSecret: '<your AccessKey Secret>',// Required. The AccessKey secret is returned when the temporary STS token is generated.
stsToken: '<your STS token>',// Required. The temporary playback credential. The STS token must be generated in advance.
domainRegion: '<your domain region>',// Required. The ID of the region in which your media asset resides, such as cn-shanghai or cn-hangzhou.
authInfo: '<your auth info>',// Required. The authorization information, which must be obtained in advance.
},function(player){
console.log('The player is created.')
});
ApsaraVideo VOD supports Alibaba Cloud proprietary cryptography and digital rights management (DRM) encryption. For more information about how to play encrypted videos, see Play an encrypted video.
Playback of live streams
To stream content from URLs, you must set the source parameter to the streaming URL and the isLive parameter to true. The streaming URL can be a third-party streaming URL or a streaming URL that is generated in ApsaraVideo Live. The ApsaraVideo Live console provides a URL generator to help you generate streaming URLs. For more information, see URL generator.
Sample code for streaming from URLs
<script>
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,// Specify whether the content that you want to play is a live steam.
},function(player){
console.log('The player is created.')
});
</script>
For more information about how to play DRM-encrypted streams, see Play an encrypted video.
Real-Time Streaming (RTS) playback
RTS allows you to play 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 RTS SDK. By default, the latest version of RTS SDK is integrated. You can also specify the RTS SDK version by configuring the parameter. For example, set rtsVersion: '2.2.1'.
Sample code for playback from RTS URLs
<script>
var player = new Aliplayer({
id: 'J_prismPlayer',
source: '<your play URL>',// The RTS playback URL. The artc:// protocol is used.
isLive: true,// Specify whether the content that you want to play is a live steam.
// rtsVersion: 'x.x.x', // Specify the version of RTS SDK.
},function(player){
console.log('The player is created.')
});
</script>