From 22b10e3ffa974187caa9d9a77088f5f5ce9e3243 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Mon, 13 May 2013 18:05:01 -0400 Subject: [PATCH] Implement #123 --- channel.js | 1 + www/assets/js/callbacks.js | 2 +- www/assets/js/media.js | 77 ++++++++++++++++++++++++++------------ 3 files changed, 56 insertions(+), 24 deletions(-) diff --git a/channel.js b/channel.js index f1c4252c..33bfdeeb 100644 --- a/channel.js +++ b/channel.js @@ -1011,6 +1011,7 @@ Channel.prototype.tryUpdate = function(user, data) { } this.media.currentTime = data.currentTime; + this.media.paused = data.paused; this.sendAll("mediaUpdate", this.media.timeupdate()); } diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index be9a936e..d743bec6 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -436,7 +436,7 @@ Callbacks = { socket.emit("mediaUpdate", { id: PLAYER.id, currentTime: seconds, - paused: false, + paused: PLAYER.paused, type: PLAYER.type }); }); diff --git a/www/assets/js/media.js b/www/assets/js/media.js index 6c2fa178..c5a8610f 100644 --- a/www/assets/js/media.js +++ b/www/assets/js/media.js @@ -48,6 +48,9 @@ Media.prototype.initYouTube = function() { socket.emit("playerReady"); }, onStateChange: function(ev) { + PLAYER.paused = (ev.data == YT.PlayerState.PAUSED); + if(PLAYER.paused) { + } if(LEADER && ev.data == YT.PlayerState.ENDED) { socket.emit("playNext"); } @@ -94,6 +97,7 @@ Media.prototype.initVimeo = function() { this.player = $f(iframe[0]); $f(iframe[0]).addEvent("ready", function() { + socket.emit("playerReady"); this.player = $f(iframe[0]); this.player.api("play"); @@ -102,6 +106,14 @@ Media.prototype.initVimeo = function() { socket.emit("playNext"); } }); + + this.player.addEvent("pause", function() { + PLAYER.paused = true; + }); + + this.player.addEvent("play", function() { + PLAYER.paused = false; + }); }.bind(this)); this.load = function(data) { @@ -135,11 +147,23 @@ Media.prototype.initDailymotion = function() { params: {autoplay: 1} }); - this.player.addEventListener("ended", function(e) { - if(LEADER) { - socket.emit("playNext"); - } - }); + this.player.addEventListener("apiready", function(e) { + socket.emit("playerReady"); + this.player.addEventListener("ended", function(e) { + if(LEADER) { + socket.emit("playNext"); + } + }); + + this.player.addEventListener("pause", function(e) { + PLAYER.paused = true; + }); + + this.player.addEventListener("playing", function(e) { + PLAYER.paused = false; + }); + }.bind(this)); + this.load = function(data) { this.id = data.id; @@ -174,13 +198,25 @@ Media.prototype.initSoundcloud = function() { iframe.css("border", "none"); this.player = SC.Widget("ytapiplayer"); - this.player.load(this.id, {auto_play: true}); - this.player.bind(SC.Widget.Events.FINISH, function() { - if(LEADER) { - socket.emit("playNext"); - } - }); + this.player.bind(SC.Widget.Events.READY, function() { + socket.emit("playerReady"); + this.player.load(this.id, {auto_play: true}); + + this.player.bind(SC.Widget.Events.PAUSE, function() { + PLAYER.paused = true; + }); + + this.player.bind(SC.Widget.Events.PLAY, function() { + PLAYER.paused = false; + }); + + this.player.bind(SC.Widget.Events.FINISH, function() { + if(LEADER) { + socket.emit("playNext"); + } + }); + }.bind(this)); this.load = function(data) { this.id = data.id; @@ -319,6 +355,9 @@ Media.prototype.update = function(data) { if(data.paused) { this.pause(); } + else if(!this.paused) { + this.play(); + } if(LEADER) { return; } @@ -326,20 +365,12 @@ Media.prototype.update = function(data) { var time = data.currentTime; var diff = time - seconds || time; - var a = USEROPTS.sync_accuracy + 1; - // If 2 updates in a row have lag, compensate for buffering - if(diff >= a && diff <= a*3 && this.diff >= a && this.diff <= a*3) { - this.seek(time + diff); + if(diff > USEROPTS.sync_accuracy) { + this.seek(time); } - else if(diff < -USEROPTS.sync_accuracy || diff >= USEROPTS.sync_accuracy) { - if(diff < 0) { - this.seek(time + 0.5); - } - else { - this.seek(time); - } + else if(diff < -USEROPTS.sync_accuracy) { + this.seek(time + 1); } - this.diff = diff; }.bind(this)); }