How do I use javascript to automatically switch to a backup live stream if primary fails in JWPlayer?

余生颓废 提交于 2020-01-06 18:36:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!