JW Player and Streaming: automatic reconnection

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- Filament.io 0 Flares ×

Every time I have to work with the beautiful JW Player I discover a new interesting feature. Have you ever had disconnection problems when using it with online streaming service?

Automatic Reconnection
Automatic Reconnection

Personally I had many! I used it for a web radio streaming service on RDS.IT and I had to solve the continuous disconnections from the stream that usually stop the playing.

So JW Player helps us with a very nice Events managing that can be used within a trick:
when an ONTIME POSITION is undefined (typically when a streaming connection breaks up) we can fire a reconnection method.

Let’s see how:

[code lang=”js”]
////////////////// JAVASCRIPT ////////////////////
// CALL THIS AFTER JWPLAYER.JS INSIDE YOUR HTML //
var timer;
jwplayer("myPlayerElement").setup({
primary: ‘html5’,
sources: [{
file: "http://myplaylistM3U8.com/playlist.m3u8"
}],
events: {
onPlay: function(event) {
clearInterval(timer);
},
onTime: function(event) {
if (typeof event.position==="undefined")
{
clearInterval(timer);
jwplayer("myPlayerElement").stop();
timer=setInterval(function()
{
jwplayer("myPlayerElement").play();
},5000);
}
}
}
});
[/code]

The trick is the following one: after an undefined onTime event (that fires if client disconnected to the stream) I clear all the previous time intervals, stop the player and set up a new interval that fires a new play attempt every 5 seconds (or whatever). When a successful onPlay event is fired I clear the time interval so not to have any other connection try.

That’s all. Hope it can help.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- Filament.io 0 Flares ×