Stop or pause after a specified time with the Wowza Player JavaScript API

Learn how to use the Wowza™ Player JavaScript API to control playback. In this specific example, see how to pause or end playback in Wowza Player after a specified length of time.

Example HTML


Note: See the Wowza Player JavaScript API Reference to get started with the Wowza Player API.

In the following code example, we use the onPlayheadTime method to attach an event listener that’s called at 500-millisecond intervals throughout playback. At each interval, the playheadTimeListener callback function prints the current playhead time (in milliseconds) to the console. If the current playhead time is greater than or equal to 30 seconds, the event listener is removed and the stream either stops or pauses, depending on whether you use the finish or pause methods.

Note: The pause method is only applicable for VOD streams and live nDVR streams, not live streams without nDVR.
<html>
<head>
  <script type="text/javascript" src="//player.wowza.com/player/latest/wowzaplayer.min.js"></script>
</head>

<body>
<div id="playerElement" style="width:100%; height:0; padding:0 0 56.25% 0"></div>
<script type="text/javascript">
  var myPlayer = WowzaPlayer.create('playerElement',
    {
    "license":"XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXX",
    "sourceURL":"http://IP:1935/vod/[file_type_prefix:filename]/playlist.m3u8"
    }
  );

  //Stop the player at or after 30 seconds 
  playheadTimeListener = function ( playheadTimeEvent ) {
    console.log('Time: ' + playheadTimeEvent.time);
    if (playheadTimeEvent.time >= 30000) {

    myPlayer.removeOnPlayheadTime( playheadTimeListener );
    myPlayer.finish();
    //Can alternatively use myPlayer.pause(); for VOD or live nDVR streams to pause the stream.
    }
  };

  //Listen for every 500 milliseconds, then execute the callback function above.
  myPlayer.onPlayheadTime(playheadTimeListener);
</script>
<body>
</html>