Continue refactoring clientside loader

- Vimeo flash
- Dailymotion
- Soundcloud
- Fix a lot of JSHint stuff
This commit is contained in:
Calvin Montgomery 2013-08-05 00:19:10 -04:00
parent 0a00d059e8
commit 39ea0c9207
1 changed files with 230 additions and 333 deletions

View File

@ -106,8 +106,8 @@ var YouTubePlayer = function (data) {
//socket.emit("playerReady"); //socket.emit("playerReady");
}, },
onStateChange: function(ev) { onStateChange: function(ev) {
if(PLAYER.paused && ev.data != YT.PlayerState.PAUSED if(PLAYER.paused && ev.data != YT.PlayerState.PAUSED ||
|| !PLAYER.paused && ev.data == YT.PlayerState.PAUSED) { !PLAYER.paused && ev.data == YT.PlayerState.PAUSED) {
self.paused = (ev.data == YT.PlayerState.PAUSED); self.paused = (ev.data == YT.PlayerState.PAUSED);
if(CLIENT.leader) if(CLIENT.leader)
sendVideoUpdate(); sendVideoUpdate();
@ -130,17 +130,17 @@ var YouTubePlayer = function (data) {
self.player.setPlaybackQuality(VIDEOQUALITY); self.player.setPlaybackQuality(VIDEOQUALITY);
self.videoId = data.id; self.videoId = data.id;
} }
} };
self.pause = function() { self.pause = function() {
if(self.player.pauseVideo) if(self.player.pauseVideo)
self.player.pauseVideo(); self.player.pauseVideo();
} };
self.play = function() { self.play = function() {
if(self.player.playVideo) if(self.player.playVideo)
self.player.playVideo(); self.player.playVideo();
} };
self.isPaused = function(callback) { self.isPaused = function(callback) {
if(self.player.getPlayerState) { if(self.player.getPlayerState) {
@ -149,19 +149,19 @@ var YouTubePlayer = function (data) {
} else { } else {
callback(false); callback(false);
} }
} };
self.getTime = function(callback) { self.getTime = function(callback) {
if(self.player.getCurrentTime) if(self.player.getCurrentTime)
callback(self.player.getCurrentTime()); callback(self.player.getCurrentTime());
} };
self.seek = function(time) { self.seek = function(time) {
if(self.player.seekTo) if(self.player.seekTo)
self.player.seekTo(time, true); self.player.seekTo(time, true);
} };
}); });
} };
var VimeoPlayer = function (data) { var VimeoPlayer = function (data) {
var self = this; var self = this;
@ -201,28 +201,28 @@ var VimeoPlayer = function (data) {
sendVideoUpdate(); sendVideoUpdate();
}); });
}.bind(self)); }.bind(self));
} };
self.init(); self.init();
self.load = function (data) { self.load = function (data) {
self.videoId = data.id; self.videoId = data.id;
self.init(); self.init();
} };
self.pause = function () { self.pause = function () {
if(self.player && self.player.api) if(self.player && self.player.api)
self.player.api("pause"); self.player.api("pause");
} };
self.play = function () { self.play = function () {
if(self.player && self.player.api) if(self.player && self.player.api)
self.player.api("play"); self.player.api("play");
} };
self.isPaused = function (callback) { self.isPaused = function (callback) {
callback(self.paused); callback(self.paused);
} };
self.getTime = function (callback) { self.getTime = function (callback) {
if(self.player && self.player.api) { if(self.player && self.player.api) {
@ -231,160 +231,21 @@ var VimeoPlayer = function (data) {
callback(parseFloat(time)); callback(parseFloat(time));
}); });
} }
} };
self.seek = function(time) { self.seek = function(time) {
if(self.player && self.player.api) if(self.player && self.player.api)
self.player.api("seekTo", time); self.player.api("seekTo", time);
} };
}); });
} };
Player.prototype.nullPlayer = function() { var VimeoFlashPlayer = function (data) {
this.player = null; var self = this;
this.load = function(data) { } self.videoId = data.id;
this.play = function() { } self.init = function () {
this.pause = function() { } removeOld();
this.getTime = function(callback) { } var url = "http://vimeo.com/moogaloop.swf?clip_id="+self.videoId;
this.seek = function(time) { }
}
Player.prototype.initYouTube = function() {
this.removeOld();
this.player = new YT.Player("ytapiplayer", {
height: VHEIGHT,
width: VWIDTH,
videoId: this.id,
playerVars: {
autohide: 1, // Autohide controls
autoplay: 1, // Autoplay video
controls: 1, // Show controls
iv_load_policy: 3, // No annotations
modestbranding: 1, // No logo
rel: 0 // No related videos
},
events: {
onReady: function() {
socket.emit("playerReady");
},
onStateChange: function(ev) {
if(PLAYER.paused && ev.data != YT.PlayerState.PAUSED
|| !PLAYER.paused && ev.data == YT.PlayerState.PAUSED) {
PLAYER.paused = (ev.data == YT.PlayerState.PAUSED);
sendVideoUpdate();
}
else {
PLAYER.paused = (ev.data == YT.PlayerState.PAUSED);
}
if(CLIENT.leader && ev.data == YT.PlayerState.ENDED) {
socket.emit("playNext");
}
}
}
});
$("#ytapiplayer").css("border", "none");
this.load = function(data) {
if(this.player.loadVideoById) {
this.player.loadVideoById(data.id, data.currentTime);
if(VIDEOQUALITY)
this.player.setPlaybackQuality(VIDEOQUALITY);
this.id = data.id;
}
}
this.pause = function() {
if(this.player.pauseVideo)
this.player.pauseVideo();
}
this.play = function() {
if(this.player.playVideo)
this.player.playVideo();
}
this.isPaused = function(callback) {
callback(this.player.getPlayerState() != YT.PlayerState.PLAYING);
}
this.getTime = function(callback) {
if(this.player.getCurrentTime)
callback(this.player.getCurrentTime());
}
this.seek = function(time) {
if(this.player.seekTo)
this.player.seekTo(time, true);
}
}
Player.prototype.initVimeo = function() {
var iframe = $("<iframe/>").insertBefore($("#ytapiplayer"));
$("#ytapiplayer").remove();
iframe.attr("id", "ytapiplayer");
iframe.attr("width", VWIDTH);
iframe.attr("height", VHEIGHT);
iframe.attr("src", "http://player.vimeo.com/video/"+this.id+"?api=1&player_id=ytapiplayer");
iframe.attr("webkitAllowFullScreen", "");
iframe.attr("mozallowfullscreen", "");
iframe.attr("allowFullScreen", "");
iframe.css("border", "none");
this.player = $f(iframe[0]);
$f(iframe[0]).addEvent("ready", function() {
socket.emit("playerReady");
this.player = $f(iframe[0]);
this.player.api("play");
this.player.addEvent("finish", function() {
if(CLIENT.leader) {
socket.emit("playNext");
}
});
this.player.addEvent("pause", function() {
PLAYER.paused = true;
sendVideoUpdate();
});
this.player.addEvent("play", function() {
PLAYER.paused = false;
sendVideoUpdate();
});
}.bind(this));
this.load = function(data) {
this.id = data.id;
this.initVimeo();
}
this.pause = function() {
this.player.api("pause");
}
this.play = function() {
this.player.api("play");
}
this.isPaused = function(callback) {
callback(this.paused);
}
this.getTime = function(callback) {
// Vimeo api returns time as a string because fuck logic
this.player.api("getCurrentTime", function(time) {
callback(parseFloat(time));
});
}
this.seek = function(time) {
this.player.api("seekTo", time);
}
}
Player.prototype.initVimeoFlash = function() {
this.removeOld();
var url = "http://vimeo.com/moogaloop.swf?clip_id="+this.id;
url += "&" + [ url += "&" + [
"server=vimeo.com", "server=vimeo.com",
"api=2", "api=2",
@ -402,179 +263,215 @@ Player.prototype.initVimeoFlash = function() {
allowfullscreen: true, allowfullscreen: true,
allowScriptAccess: "always" allowScriptAccess: "always"
}; };
swfobject.embedSWF(url swfobject.embedSWF(url,
, "ytapiplayer" "ytapiplayer",
, VWIDTH VWIDTH,
, VHEIGHT VHEIGHT,
, "9.0.0" "9.0.0",
, "expressInstall.swf" "expressInstall.swf",
, flashvars flashvars,
, params); params);
this.player = $("#ytapiplayer")[0]; self.player = $("#ytapiplayer")[0];
waitUntilDefined(this.player, "api_addEventListener", function () { waitUntilDefined(self.player, "api_addEventListener", function () {
this.player.api_addEventListener("ready", function () { self.player.api_addEventListener("ready", function () {
socket.emit("playerReady"); //socket.emit("playerReady");
this.player.api_play(); self.player.api_play();
this.player.api_addEvent("finish", function () { self.player.api_addEvent("finish", function () {
if(CLIENT.leader) if(CLIENT.leader)
socket.emit("playNext"); socket.emit("playNext");
}); });
this.player.api_addEvent("pause", function() { self.player.api_addEvent("pause", function () {
PLAYER.paused = true; PLAYER.paused = true;
if(CLIENT.leader)
sendVideoUpdate(); sendVideoUpdate();
}); });
this.player.api_addEvent("play", function() { self.player.api_addEvent("play", function () {
PLAYER.paused = false; PLAYER.paused = false;
if(CLIENT.leader)
sendVideoUpdate(); sendVideoUpdate();
}); });
}.bind(this));
}); });
});
};
this.load = function(data) { self.init();
this.id = data.id;
this.initVimeoFlash();
}
this.pause = function() { self.load = function (data) {
this.player.api_pause(); self.videoId = data.id;
} self.init();
};
this.play = function() { self.pause = function () {
this.player.api_play(); if(self.player && self.player.api_pause)
} self.player.api_pause();
};
this.isPaused = function(callback) { self.play = function () {
callback(this.paused); if(self.player && self.player.api_play)
} self.player.api_play();
};
this.getTime = function(callback) { self.isPaused = function (callback) {
var t = parseFloat(this.player.api_getCurrentTime()); callback(self.paused);
};
self.getTime = function (callback) {
if(self.player && self.player.api_getCurrentTime) {
var t = parseFloat(self.player.api_getCurrentTime());
callback(t); callback(t);
} }
};
this.seek = function(time) { self.seek = function (time) {
this.player.api_seekTo(time); if(self.player.api_seekTo);
} self.player.api_seekTo(time);
} };
};
Player.prototype.initDailymotion = function() { var DailymotionPlayer = function (data) {
this.removeOld(); var self = this;
this.player = DM.player("ytapiplayer", { waitUntilDefined(window, "DM", function () {
video: this.id, removeOld();
self.videoId = data.id;
self.player = DM.player("ytapiplayer", {
video: data.id,
width: parseInt(VWIDTH), width: parseInt(VWIDTH),
height: parseInt(VHEIGHT), height: parseInt(VHEIGHT),
params: { autoplay: 1 } params: { autoplay: 1 }
}); });
this.player.addEventListener("apiready", function(e) { self.player.addEventListener("apiready", function (e) {
socket.emit("playerReady"); //socket.emit("playerReady");
this.player.addEventListener("ended", function(e) { self.player.addEventListener("ended", function (e) {
if(CLIENT.leader) { if(CLIENT.leader) {
socket.emit("playNext"); socket.emit("playNext");
} }
}); });
this.player.addEventListener("pause", function(e) { self.player.addEventListener("pause", function (e) {
PLAYER.paused = true; PLAYER.paused = true;
if(CLIENT.leader)
sendVideoUpdate(); sendVideoUpdate();
}); });
this.player.addEventListener("playing", function(e) { self.player.addEventListener("playing", function (e) {
PLAYER.paused = false; PLAYER.paused = false;
if(CLIENT.leader)
sendVideoUpdate(); sendVideoUpdate();
}); });
}.bind(this)); });
});
self.load = function (data) {
self.videoId = data.id;
if(self.player && self.player.api)
self.player.api("load", data.id);
};
this.load = function(data) { self.pause = function () {
this.id = data.id; if(self.player && self.player.api)
this.player.api("load", data.id); self.player.api("pause");
} };
this.pause = function() { self.play = function () {
this.player.api("pause"); if(self.player && self.player.api)
} self.player.api("play");
};
this.play = function() { self.isPaused = function (callback) {
this.player.api("play"); callback(self.paused);
} };
this.isPaused = function(callback) { self.getTime = function (callback) {
callback(this.paused); if(self.player)
} callback(self.player.currentTime);
};
this.getTime = function(callback) { self.seek = function (seconds) {
callback(this.player.currentTime); if(self.player && self.player.api)
} self.player.api("seek", seconds);
};
};
this.seek = function(seconds) { var SoundcloudPlayer = function (data) {
this.player.api("seek", seconds); var self = this;
} self.videoId = data.id;
} waitUntilDefined(window, "SC", function () {
Player.prototype.initSoundcloud = function() {
unfixSoundcloudShit(); unfixSoundcloudShit();
var iframe = $("<iframe/>").insertBefore($("#ytapiplayer")); var iframe = $("<iframe/>");
$("#ytapiplayer").remove(); removeOld(iframe);
iframe.attr("id", "ytapiplayer"); iframe.attr("id", "ytapiplayer");
iframe.attr("src", "https://w.soundcloud.com/player/?url=" + this.id); iframe.attr("src", "https://w.soundcloud.com/player/?url="+self.videoId);
iframe.css("width", "100%").attr("height", "166"); iframe.css("width", "100%").attr("height", "166");
iframe.css("border", "none"); iframe.css("border", "none");
this.player = SC.Widget("ytapiplayer"); self.player = SC.Widget("ytapiplayer");
this.player.bind(SC.Widget.Events.READY, function() { self.player.bind(SC.Widget.Events.READY, function () {
socket.emit("playerReady"); //socket.emit("playerReady");
this.player.load(this.id, {auto_play: true}); self.player.load(self.videoId, { auto_play: true });
this.player.bind(SC.Widget.Events.PAUSE, function() { self.player.bind(SC.Widget.Events.PAUSE, function () {
PLAYER.paused = true; PLAYER.paused = true;
if(CLIENT.leader)
sendVideoUpdate(); sendVideoUpdate();
}); });
this.player.bind(SC.Widget.Events.PLAY, function() { self.player.bind(SC.Widget.Events.PLAY, function () {
PLAYER.paused = false; PLAYER.paused = false;
if(CLIENT.leader)
sendVideoUpdate(); sendVideoUpdate();
}); });
this.player.bind(SC.Widget.Events.FINISH, function() { self.player.bind(SC.Widget.Events.FINISH, function () {
if(CLIENT.leader) { if(CLIENT.leader) {
socket.emit("playNext"); socket.emit("playNext");
} }
}); });
}.bind(this)); }.bind(self));
});
this.load = function(data) { self.load = function (data) {
this.id = data.id; self.videoId = data.id;
this.length = data.length; if(self.player && self.player.load)
this.player.load(data.id, {auto_play: true}); self.player.load(data.id, { auto_play: true });
} };
this.pause = function() { self.pause = function () {
this.player.pause(); if(self.player && self.player.pause)
} self.player.pause();
};
this.play = function() { self.play = function () {
this.player.play(); if(self.player && self.player.play)
} self.player.play();
};
this.isPaused = this.player.isPaused; self.isPaused = function (callback) {
if(self.player && self.player.isPaused)
self.player.isPaused(callback);
else
callback(false);
};
this.getTime = function(callback) { self.getTime = function (callback) {
this.player.getPosition(function(pos) { if(self.player && self.player.getPosition) {
self.player.getPosition(function (pos) {
callback(pos / 1000); callback(pos / 1000);
}); });
} }
};
this.seek = function(seconds) { self.seek = function (seconds) {
this.player.seekTo(seconds * 1000); if(self.player && self.player.seekTo)
} self.player.seekTo(seconds * 1000);
} };
};
Player.prototype.initLivestream = function() { Player.prototype.initLivestream = function() {
this.removeOld(); this.removeOld();