Using AVPlay

This topic describes how your application can play media content using the AVPlay API.

In general, common media formats can be played using the HTML5 video and audio elements. To play media content with features not supported in HTML5, such as adaptive streaming, additional subtitle formats, and 4K UHD video, use the Samsung Product AVPlay API.

For information on the supported media containers, streaming format and DRM combinations, and network protocols, see the General Specifications.

AVPlay Life-cycle

An AVPlay instance has various states, which determine the actions you can take. You must also pay attention to the state limitations for the AVPlay API methods.

The following table lists the possible AVPlay instance states.

Table 1. AVPlay instance states
State Description
NONE Before the instance is created, or after it is removed with the close() method.
IDLE After the instance is created by opening the media with the open() method.
READY During and after media buffering.
In this state you can, for example, use the getDuration() method to retrieve the duration of the media.
PLAYING When playback is in progress.
In this state you can, for example, use the getCurrentTime() method to retrieve the current time of the media.
PAUSED When playback is paused with the pause() method.

The following figure shows the state changes and life-cycle of an AVPlay instance, and the playback control operations that drive state transitions.

Figure 1. AVPlay instance life-cycle

Figure 1. AVPlay instance life-cycle


To enable your application to use the functionalities of the AVPlay API:

  • To use the AVPlay API, include the WebAPI library in the “index.html” file:
     <script type="text/javascript" src="$WEBAPIS/webapis/webapis.js"></script>
  • To access external network resources through the application, set the appropriate content security policy (CSP) in the “config.xml” file.

Since 2015 models, the privilege is not used in TV Web applications.

Playing Media

To play media using the AVPlay API:

  1. Create an element for media playback, using an object element with the "application/avplayer" type attribute:

    var objElem = document.createElement('object');
    objElem.type = 'application/avplayer';
    //Adjust the size and position of the media display area 
    //by changing the CSS style attribute = 100 + 'px'; = 200 + 'px'; = 600 + 'px'; = 400 + 'px';
    //Append the object element to your document
  2. Open a media file using the open() method.
    The AVPlay API supports absolute local paths and remote network URIs. Relative local paths are not supported.'yourMediaURI');

    After the media file is successfully opened, the AVPlay instance is created in the IDLE state.

  3. Define the event handlers using the setListener() method. The following table lists the available event handlers.

    Table 2. Event methods
    Event Handler Description
    onbufferingstart() Media data buffering has started.
    onbufferingprogress() Media data buffering progress percentage.
    onbufferingcomplete() Media data buffering has completed.
    onstreamcompleted() Media playback has completed.
    oncurrentplaytime() Current playback time in the PLAYING state, in milliseconds.
    onerror() An error has occurred during media playback.
    onevent() Some other event is received by the player.
    onsubtitlechange() Subtitle text has changed.
    ondrmevent() DRM information is detected by the player.
    var listener = {
      onbufferingstart: function() {
        console.log("Buffering start.");
      onbufferingprogress: function(percent) {
        console.log("Buffering progress data : " + percent);
      onbufferingcomplete: function() {
        console.log("Buffering complete.");
      onstreamcompleted: function() {
        console.log("Stream Completed");
      oncurrentplaytime: function(currentTime) {
        console.log("Current playtime: " + currentTime);
      onerror: function(eventType) {
        console.log("event type error : " + eventType);
      onevent: function(eventType, eventData) {
        console.log("event type: " + eventType + ", data: " + eventData);
      onsubtitlechange: function(duration, text, data3, data4) {
        console.log("subtitleText: " + text);
      ondrmevent: function(drmEvent, drmData) {
        console.log("DRM callback: " + drmEvent + ", data: " + drmData);
  4. For video media, set the media display area using the setDisplayRect() method.
    The setDisplayRect() method takes 4 parameters: the position from the left side of the screen, the position from the top of the screen, the width, and the height. For the purpose of the setDisplayRect() method parameters, the TV screen resolution is always treated as 1920x1080 px, regardless of the application resolution or viewport size.
    If the application resolution is 1920x1080 px, simply set the same position values as defined in the object element style attribute. Otherwise, you must calculate the values for the setDisplayRect() method parameters.

    // For example,  video positon is 
    // left: 100 px / top: 200 px / width: 600 px / height: 400 px
    // Case 1: Application resolution 1920x1080 px
    // Case 2: Other application resolution
    // Base resolution of avplay
    var avplayBaseWidth = 1920;
    // Calculate ratio to base resolution
    var ratio = avplayBaseWidth / window.document.documentElement.clientWidth;
    // Convert rectangle to base resolution
    var newLeft = 100 * ratio;
    var newTop = 200 * ratio;
    var newWidth = 600 * ratio;
    var newHeight = 400 * ratio;

    If you use the setDisplayRect() method to change the media display area size or position during media playback, the object element style attribute must also be changed correspondingly.

    By default, video is displayed full screen within the media display area. To fit the video to the media display area:

  5. Prepare the media for playback, synchronously or asynchronously:

    • To prepare the media synchronously, use the prepare() method.
      Because preparation can involve fetching and decoding media data, the prepare() method can take a long time to execute. Do not call it from your application’s UI thread, as it causes the UI to hang until execution is finished.
    • To prepare the media asynchronously, use the prepareAsync() method.
      This method starts preparing in the background and returns immediately. When the media is finished preparing, the successCallback() method is invoked.
      var successCallback = function() {
        console.log('The media has finished preparing');
      var errorCallback = function() {
        console.log('The media has failed to prepare');

    When the AVPlay instance starts preparing the media, the onbufferingstart() event handler is invoked, and the AVPlay instance enters the READY state.

  6. To manage starting and stopping playback:

    1. Start playback using the play() method:; 
      When playback starts, the oncurrentplaytime() event handler is invoked, and the AVPlay instance enters the PLAYING state.
    2. Pause playback using the pause() method:
      The AVPlay instance enters the PAUSED state. Playback can be resumed using the play() method.
    3. When playback is complete, stop the player:
      The AVPlay instance enters the IDLE state. The instance retains its configuration, such as the media URI, display area, and event methods. To replay the media, you can simply call the prepare() and play() methods again.

      Failure to stop playback after the media has finished playing causes the media player window to show the final frame continuously.

  7. To remove the AVPlay instance, call the close() method.

Managing Media Playback

Using the AVPlay API, you can implement functionalities such as jumping to a specific time in the media, adjusting the playback rate, and switching audio tracks.

Media Seek

You can jump to a specific time in the media based on an absolute time or relative time:

  • To jump based on an absolute time, use the seekTo() method:

    1. Retrieve the current playback time using the getCurrentTime() method.
    2. Set the time to jump to. The new time must be a positive value, which is less than the duration of the media.
      //Media seek during playback
      var successCallback = function() {
        console.log('Media seek successful');
      var errorCallback = function() {
        console.log('Media seek failed');
      //Jump forward by 5000 ms
      var currentTime = webapis.avplay.getCurrentTime();
      var newTime = currentTime + 5000;

    You can also set the start position for media playback by calling the seekTo() method when the AVPlay instance is in the IDLE state.

      //Change playback start time
      var successCallback = function() {
        console.log('Media seek successful');
      var errorCallback = function() {
        console.log('Media seek failed');
      //Move the playback start time to 10 minutes
  • To jump based on a relative time, use the jumpForward() and jumpBackward() methods:

    //Media seek during playback
    var successCallback = function() {
      console.log('Media seek successful');
    var errorCallback = function() {
      console.log('Media seek failed');
    //Case 1 Fast-forward by 5000 ms
    //Case 2 Rewind by 5000 ms

    You must check that the value of the parameter is within the valid range, based on the current playback time.

Playback Rate Control (Trick Play)

You can control the playback rate of the media using the setSpeed() method to set a multiplier for the playback rate. Positive parameter values play the media forwards, while negative values cause the media to play in reverse.
For example, to play the media at double speed:


The maximum playback rate depends on the streaming format.

Table 3. Playback rates for streaming formats
Streaming Format Playback Rate Range
Smooth Streaming -16x ~ +16x
HTTP(S) -8x ~ +8x
MPEG-DASH -16x ~ +16x
HTTP Live Streaming (HLS) Since 2017 models: -16x ~ +16x
for content with "EXT-X-I-FRAME-STREAM_INF" tag only

Audio Track Switching

To switch between multiple audio tracks during media playback:

  1. Retrieve the audio track list.
    The getTotalTrackInfo() method of the AVPlay API can only be called when the AVPlay instance is in the READY, PLAYING, or PAUSED states. It returns an array of objects for all tracks in the media.
    Each object has 3 properties:

    • type: The AUDIO value identifies the track as an audio track.
    • index: The index number for the track.
    • extra_info: Additional information about the track, such as language information and number of channels.
    var totalTrackInfo = webapis.avplay.getTotalTrackInfo();
    for (var i=0; i<totalTrackInfo.length;i++)
      if (totalTrackInfo.type =='AUDIO')
        console.log('Find audio track.');
        console.log('audio track index is ' + totalTrackInfo.index);
        console.log('audio track language is ' + totalTrackInfo.extra_info.language);
  2. Select the audio track.
    Call the setSelectTrack() method with the track type and index number as parameters:

    // Select audio track with index 2

State Limitations for AVPlay API Methods

The following table lists the AVPlay API methods and their valid states. Unless otherwise specified, calling the method does not change the state of the AVPlay instance.

Table 4. AVPlay API method state limitations
MethodValid StatesNotes
open()NONE, IDLEAfter a successful call during a valid state, the instance enters the IDLE state.
prepare()IDLE, READYAfter a successful call during a valid state, the instance enters the READY state.
play()READY, PLAYING, PAUSEDAfter a successful call during a valid state, the instance enters the PLAYING state.
pause()PLAYING, PAUSEDAfter a successful call during a valid state, the instance enters the PAUSED state.
stop()anyAfter a successful call, the instance enters the IDLE state.
close()After a successful call, the instance is removed and enters the NONE state.
set360Rotation()READY, PLAYING, PAUSED
setDrm()NONE, IDLENONE state is supported for Verimatrix "GetUID" operation only
unsetSoundAnalysisListener()IDLE, READY, PLAYING, PAUSED
setSubtitlePosition()PLAYING, PAUSED
setSelectTrack()READY, PLAYING, PAUSEDREADY state is supported for Smooth Streaming only
getTotalTrackInfo()READY state is supported for synchronous prepare method only
getStreamingProperty()READY, PLAYING, PAUSED