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

  1. Add a specified .js file to your project.
    ApsaraVideo Player SDK for Web does not depend on frontend .js libraries. You can add a specified .js file to your project to initialize the player.
    • Add an adaptive .js file

      The adaptive .js file contains the adaptation logic for the Flash player and HTML5 player on multiple mobile devices. After the .js file is added, ApsaraVideo Player SDK for Web automatically selects a player according to the environment.

      <head>
        <link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.9.23/skins/default/aliplayer-min.css" />  // Optional. If you want to use the HTML5 player for playback, you must add the .css file to your project. 
        <script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.9.23/aliplayer-min.js"></script>  // Required. You must add the .js file. 
      </head>
      Note After the adaptive .js file is added, set useFlashPrism=true to use the Flash player or set useH5Prism=true to use the HTML5 player.
    • Add a single-mode .js file
      If you use only the Flash player or HTML5 player, you can add the .js file for only the player that you use to your project to reduce the file size.
      • The following sample code provides an example on the .js file if you want to use the HTML5 player:
        <head>
          <link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.9.23/skins/default/aliplayer-min.css" />  // Required. You must add the .css file to use the HTML5 player. 
          <script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.9.23/aliplayer-h5-min.js"></script>  // Required. You must add the .js file for the HTML5 player. 
        </head>
      • The following sample code provides an example on the .js file if you want to use the Flash player:
        <head>
          <script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.9.23/aliplayer-flash-min.js"></script>  // Required. You must add the .js file for the Flash player. 
        </head>
    To use the Flash player in Internet Explorer 8, you must add the reference to the json.min.js file in the browser. The following sample code provides an example:
    https://g.alicdn.com/de/prismplayer/2.9.23/json/json.min.js
  2. Specify a container for the player.
    <body>
      <div id="J_prismPlayer"></div>
    </body>
  3. Instantiate the player.
    • ApsaraVideo Player SDK for Web supports playback from URLs, playback based on VID and PlayAuth, Security Token Service (STS)-based playback, playback by using ApsaraVideo Media Processing (MPS), and encrypted playback. We recommend that you play videos based on VID and PlayAuth. For more information, see Playback of VODs.
    • ApsaraVideo Player SDK for Web supports streaming from URLs and encrypted live streams. For more information, see Playback of live streams.

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>

References