This commit is contained in:
calzoneman 2015-05-02 17:37:09 -05:00
parent 391ea264f5
commit d7b69bce38
4 changed files with 38 additions and 33 deletions

View File

@ -4,12 +4,10 @@ TYPE_MAP =
window.loadMediaPlayer = (data) -> window.loadMediaPlayer = (data) ->
if data.type of TYPE_MAP if data.type of TYPE_MAP
console.log data
try try
window.PLAYER = TYPE_MAP[data.type](data) window.PLAYER = TYPE_MAP[data.type](data)
catch e catch e
console.error e console.error e
console.log(window.PLAYER)
window.handleMediaUpdate = (data) -> window.handleMediaUpdate = (data) ->
PLAYER = window.PLAYER PLAYER = window.PLAYER
@ -33,6 +31,7 @@ window.handleMediaUpdate = (data) ->
PLAYER.play() PLAYER.play()
if waiting if waiting
PLAYER.seekTo(0)
# YouTube player has a race condition that crashes the player if # YouTube player has a race condition that crashes the player if
# play(), seek(0), and pause() are called quickly without waiting # play(), seek(0), and pause() are called quickly without waiting
# for events to fire. Setting a flag variable that is checked in the # for events to fire. Setting a flag variable that is checked in the
@ -40,8 +39,8 @@ window.handleMediaUpdate = (data) ->
if PLAYER instanceof YouTubePlayer if PLAYER instanceof YouTubePlayer
PLAYER.pauseSeekRaceCondition = true PLAYER.pauseSeekRaceCondition = true
else else
PLAYER.seekTo(0)
PLAYER.pause() PLAYER.pause()
return
else if PLAYER instanceof YouTubePlayer else if PLAYER instanceof YouTubePlayer
PLAYER.pauseSeekRaceCondition = false PLAYER.pauseSeekRaceCondition = false

View File

@ -28,13 +28,16 @@ window.YouTubePlayer = class YouTubePlayer extends Player
load: (data) -> load: (data) ->
@setMediaProperties(data) @setMediaProperties(data)
if @yt if @yt and @yt.ready
@yt.loadVideoById(data.id, data.currentTime) @yt.loadVideoById(data.id, data.currentTime)
@qualityRaceCondition = true @qualityRaceCondition = true
if USEROPTS.default_quality if USEROPTS.default_quality
@yt.setPlaybackQuality(USEROPTS.default_quality) @yt.setPlaybackQuality(USEROPTS.default_quality)
else
console.error('WTF? YouTubePlayer::load() called but yt is not ready')
onReady: -> onReady: ->
@yt.ready = true
@setVolume(VOLUME) @setVolume(VOLUME)
onStateChange: (ev) -> onStateChange: (ev) ->
@ -62,20 +65,20 @@ window.YouTubePlayer = class YouTubePlayer extends Player
play: -> play: ->
@paused = false @paused = false
if @yt if @yt and @yt.ready
@yt.playVideo() @yt.playVideo()
pause: -> pause: ->
@paused = true @paused = true
if @yt if @yt and @yt.ready
@yt.pauseVideo() @yt.pauseVideo()
seekTo: (time) -> seekTo: (time) ->
if @yt if @yt and @yt.ready
@yt.seekTo(time, true) @yt.seekTo(time, true)
setVolume: (volume) -> setVolume: (volume) ->
if @yt if @yt and @yt.ready
if volume > 0 if volume > 0
# If the player is muted, even if the volume is set, # If the player is muted, even if the volume is set,
# the player remains muted # the player remains muted
@ -83,13 +86,13 @@ window.YouTubePlayer = class YouTubePlayer extends Player
@yt.setVolume(volume * 100) @yt.setVolume(volume * 100)
getTime: (cb) -> getTime: (cb) ->
if @yt if @yt and @yt.ready
cb(@yt.getCurrentTime()) cb(@yt.getCurrentTime())
else else
cb(0) cb(0)
getVolume: (cb) -> getVolume: (cb) ->
if @yt if @yt and @yt.ready
if @yt.isMuted() if @yt.isMuted()
cb(0) cb(0)
else else

View File

@ -829,11 +829,17 @@ Callbacks = {
VOLUME = 1; VOLUME = 1;
} }
function loadNext() {
if (data.type !== PLAYER.mediaType) {
loadMediaPlayer(data);
}
handleMediaUpdate(data);
}
// Persist the user's volume preference from the the player, if possible // Persist the user's volume preference from the the player, if possible
if (PLAYER && typeof PLAYER.getVolume === "function") { if (PLAYER && typeof PLAYER.getVolume === "function") {
var name = PLAYER.__proto__.constructor.name;
PLAYER.getVolume(function (v) { PLAYER.getVolume(function (v) {
console.log(name, v)
if (typeof v === "number") { if (typeof v === "number") {
if (v < 0 || v > 1) { if (v < 0 || v > 1) {
// Dailymotion's API was wrong once and caused a huge // Dailymotion's API was wrong once and caused a huge
@ -850,7 +856,11 @@ Callbacks = {
setOpt("volume", VOLUME); setOpt("volume", VOLUME);
} }
} }
loadNext();
}); });
} else {
loadNext();
} }
// Reset voteskip since the video changed // Reset voteskip since the video changed
@ -859,15 +869,6 @@ Callbacks = {
} }
$("#currenttitle").text("Currently Playing: " + data.title); $("#currenttitle").text("Currently Playing: " + data.title);
// TODO: fix this
setTimeout(function () {
if (data.type !== PLAYER.mediaType) {
loadMediaPlayer(data);
}
handleMediaUpdate(data);
}, 100);
}, },
mediaUpdate: function(data) { mediaUpdate: function(data) {

View File

@ -186,16 +186,19 @@
YouTubePlayer.prototype.load = function(data) { YouTubePlayer.prototype.load = function(data) {
this.setMediaProperties(data); this.setMediaProperties(data);
if (this.yt) { if (this.yt && this.yt.ready) {
this.yt.loadVideoById(data.id, data.currentTime); this.yt.loadVideoById(data.id, data.currentTime);
this.qualityRaceCondition = true; this.qualityRaceCondition = true;
if (USEROPTS.default_quality) { if (USEROPTS.default_quality) {
return this.yt.setPlaybackQuality(USEROPTS.default_quality); return this.yt.setPlaybackQuality(USEROPTS.default_quality);
} }
} else {
return console.error('WTF? YouTubePlayer::load() called but yt is not ready');
} }
}; };
YouTubePlayer.prototype.onReady = function() { YouTubePlayer.prototype.onReady = function() {
this.yt.ready = true;
return this.setVolume(VOLUME); return this.setVolume(VOLUME);
}; };
@ -223,26 +226,26 @@
YouTubePlayer.prototype.play = function() { YouTubePlayer.prototype.play = function() {
this.paused = false; this.paused = false;
if (this.yt) { if (this.yt && this.yt.ready) {
return this.yt.playVideo(); return this.yt.playVideo();
} }
}; };
YouTubePlayer.prototype.pause = function() { YouTubePlayer.prototype.pause = function() {
this.paused = true; this.paused = true;
if (this.yt) { if (this.yt && this.yt.ready) {
return this.yt.pauseVideo(); return this.yt.pauseVideo();
} }
}; };
YouTubePlayer.prototype.seekTo = function(time) { YouTubePlayer.prototype.seekTo = function(time) {
if (this.yt) { if (this.yt && this.yt.ready) {
return this.yt.seekTo(time, true); return this.yt.seekTo(time, true);
} }
}; };
YouTubePlayer.prototype.setVolume = function(volume) { YouTubePlayer.prototype.setVolume = function(volume) {
if (this.yt) { if (this.yt && this.yt.ready) {
if (volume > 0) { if (volume > 0) {
this.yt.unMute(); this.yt.unMute();
} }
@ -251,7 +254,7 @@
}; };
YouTubePlayer.prototype.getTime = function(cb) { YouTubePlayer.prototype.getTime = function(cb) {
if (this.yt) { if (this.yt && this.yt.ready) {
return cb(this.yt.getCurrentTime()); return cb(this.yt.getCurrentTime());
} else { } else {
return cb(0); return cb(0);
@ -259,7 +262,7 @@
}; };
YouTubePlayer.prototype.getVolume = function(cb) { YouTubePlayer.prototype.getVolume = function(cb) {
if (this.yt) { if (this.yt && this.yt.ready) {
if (this.yt.isMuted()) { if (this.yt.isMuted()) {
return cb(0); return cb(0);
} else { } else {
@ -282,14 +285,12 @@
window.loadMediaPlayer = function(data) { window.loadMediaPlayer = function(data) {
var e; var e;
if (data.type in TYPE_MAP) { if (data.type in TYPE_MAP) {
console.log(data);
try { try {
window.PLAYER = TYPE_MAP[data.type](data); return window.PLAYER = TYPE_MAP[data.type](data);
} catch (_error) { } catch (_error) {
e = _error; e = _error;
console.error(e); return console.error(e);
} }
return console.log(window.PLAYER);
} }
}; };
@ -308,12 +309,13 @@
PLAYER.play(); PLAYER.play();
} }
if (waiting) { if (waiting) {
PLAYER.seekTo(0);
if (PLAYER instanceof YouTubePlayer) { if (PLAYER instanceof YouTubePlayer) {
PLAYER.pauseSeekRaceCondition = true; PLAYER.pauseSeekRaceCondition = true;
} else { } else {
PLAYER.seekTo(0);
PLAYER.pause(); PLAYER.pause();
} }
return;
} else if (PLAYER instanceof YouTubePlayer) { } else if (PLAYER instanceof YouTubePlayer) {
PLAYER.pauseSeekRaceCondition = false; PLAYER.pauseSeekRaceCondition = false;
} }