问题
I am attempting to write a javascript function that will enable an instance of JW Player to automatically switch from a primary live HLS stream to a backup live HLS stream in the event of an error (ex: primary encoder goes down).
What I have so far:
<div id="myElement">Loading the player...</div>
var playerInstance = jwplayer("myElement");
playerInstance.setup({
file: "http://server/primary/playlist.m3u8",
width: 640,
height: 360,
title: 'Basic Video Embed',
description: 'work damn you',
});
jwplayer('myElement').on('error', function(event) {
var prime = document.getElementById("myElement").innerHTML;
var backup = prime.replace("http://server/primary/playlist.m3u8", "http://server/backup/playlist.m3u8");
document.getElementById("myElement").innerHTML = backup;
});
In my testing this doesn't work (when the primary stops, the function doesn't execute, the player just spins). I've tried ('error'), ('buffering'), ('idle'), all with the same results.
What would I change in this code to accomplish my goal? Any help is welcome.
回答1:
One of our engineers just wrote a great blog post about this solution, including code samples: https://www.jwplayer.com/blog/building-insights-video-experience/
insightsPlayer.on('error', function(event) {
playStream('error');
});
insightsPlayer.on('complete', function(event) {
playStream(currentStream);
});
If there’s an error, the player switches to the error file. The video is a 10-second still that displays an informational slide. Once that completes, the player tries to play the previously determined currentStream.
来源:https://stackoverflow.com/questions/37167560/how-do-i-use-javascript-to-automatically-switch-to-a-backup-live-stream-if-prima