Fall back to a second source with the Wowza Player JavaScript API

Learn how to use the Wowza™ Player JavaScript API to set up multiple sources for playback. This allows fallback to a secondary source if connection attempts to a primary source fail.

About fallback behavior in Wowza Player


When playback is requested, Wowza Player attempts to connect to a source URL. This source URL can be listed individually or as part of an array in a sources object. During each connection attempt, Wowza Player tries to connect to the source URL a maximum of three times. If connection to a source fails after three tries, Wowza Player checks to see whether there is a second source URL available in the sources object. If a second source is listed, Wowza Player falls back to the second source URL for playback. 

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 a sources object containing more than one sourceURL. We also use the onPlaybackAttempt method to attach an event listener that’s called when Wowza Player attempts to connect. Additionally, we use the onPlaybackSuccess and onPlaybackFailure methods to listen for whether the attempted playback succeeds or fails. When each event listener is called, a corresponding callback function prints a message to the console. If connection to the first sourceURL fails, Wowza Player falls back to the second sourceURL.

Note: Although this code example uses VOD source streams, fallback functionality is applicable for live streams, live nDVR streams, and ultra low latency streams as well.
<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",
      "sources": [
        {"sourceURL":"http://IP:1935/vod/doesnotexist.mp4/playlist.m3u8"},
        {"sourceURL":"http://IP:1935/vod/sample.mp4/playlist.m3u8"}
      ]
    }
  );

  //Print to console for attempts, successes, and failures
  function playbackAttemptListener( playbackEvent ){
    console.log('Attempt: ' + playbackEvent.url);
  }
 
  function playbackSuccessListener( playbackEvent ){
    console.log('Success: ' + playbackEvent.url);
  }

  function playbackFailureListener( playbackEvent ){
   console.log('Failure: ' + playbackEvent.url);
  }

  //Listen for playback attempts, successes, and failures and execute callback functions above.
  myPlayer.onPlaybackAttempt(playbackAttemptListener);
  myPlayer.onPlaybackSuccess(playbackSuccessListener);
  myPlayer.onPlaybackFailure(playbackFailureListener);

</script>
<body>
</html>