diff --git a/player/update.coffee b/player/update.coffee index 5982988a..536315d8 100644 --- a/player/update.coffee +++ b/player/update.coffee @@ -4,12 +4,10 @@ TYPE_MAP = window.loadMediaPlayer = (data) -> if data.type of TYPE_MAP - console.log data try window.PLAYER = TYPE_MAP[data.type](data) catch e console.error e - console.log(window.PLAYER) window.handleMediaUpdate = (data) -> PLAYER = window.PLAYER @@ -33,6 +31,7 @@ window.handleMediaUpdate = (data) -> PLAYER.play() if waiting + PLAYER.seekTo(0) # YouTube player has a race condition that crashes the player if # play(), seek(0), and pause() are called quickly without waiting # for events to fire. Setting a flag variable that is checked in the @@ -40,8 +39,8 @@ window.handleMediaUpdate = (data) -> if PLAYER instanceof YouTubePlayer PLAYER.pauseSeekRaceCondition = true else - PLAYER.seekTo(0) PLAYER.pause() + return else if PLAYER instanceof YouTubePlayer PLAYER.pauseSeekRaceCondition = false diff --git a/player/youtube.coffee b/player/youtube.coffee index ef3af95e..7f0db37c 100644 --- a/player/youtube.coffee +++ b/player/youtube.coffee @@ -28,13 +28,16 @@ window.YouTubePlayer = class YouTubePlayer extends Player load: (data) -> @setMediaProperties(data) - if @yt + if @yt and @yt.ready @yt.loadVideoById(data.id, data.currentTime) @qualityRaceCondition = true if USEROPTS.default_quality @yt.setPlaybackQuality(USEROPTS.default_quality) + else + console.error('WTF? YouTubePlayer::load() called but yt is not ready') onReady: -> + @yt.ready = true @setVolume(VOLUME) onStateChange: (ev) -> @@ -62,20 +65,20 @@ window.YouTubePlayer = class YouTubePlayer extends Player play: -> @paused = false - if @yt + if @yt and @yt.ready @yt.playVideo() pause: -> @paused = true - if @yt + if @yt and @yt.ready @yt.pauseVideo() seekTo: (time) -> - if @yt + if @yt and @yt.ready @yt.seekTo(time, true) setVolume: (volume) -> - if @yt + if @yt and @yt.ready if volume > 0 # If the player is muted, even if the volume is set, # the player remains muted @@ -83,13 +86,13 @@ window.YouTubePlayer = class YouTubePlayer extends Player @yt.setVolume(volume * 100) getTime: (cb) -> - if @yt + if @yt and @yt.ready cb(@yt.getCurrentTime()) else cb(0) getVolume: (cb) -> - if @yt + if @yt and @yt.ready if @yt.isMuted() cb(0) else diff --git a/www/js/callbacks.js b/www/js/callbacks.js index 9fe978f2..3b9a9fa1 100644 --- a/www/js/callbacks.js +++ b/www/js/callbacks.js @@ -829,11 +829,17 @@ Callbacks = { 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 if (PLAYER && typeof PLAYER.getVolume === "function") { - var name = PLAYER.__proto__.constructor.name; PLAYER.getVolume(function (v) { - console.log(name, v) if (typeof v === "number") { if (v < 0 || v > 1) { // Dailymotion's API was wrong once and caused a huge @@ -850,7 +856,11 @@ Callbacks = { setOpt("volume", VOLUME); } } + + loadNext(); }); + } else { + loadNext(); } // Reset voteskip since the video changed @@ -859,15 +869,6 @@ Callbacks = { } $("#currenttitle").text("Currently Playing: " + data.title); - - // TODO: fix this - setTimeout(function () { - if (data.type !== PLAYER.mediaType) { - loadMediaPlayer(data); - } - - handleMediaUpdate(data); - }, 100); }, mediaUpdate: function(data) { diff --git a/www/js/player-new.js b/www/js/player-new.js index 10c8376f..bbb3204d 100644 --- a/www/js/player-new.js +++ b/www/js/player-new.js @@ -186,16 +186,19 @@ YouTubePlayer.prototype.load = function(data) { this.setMediaProperties(data); - if (this.yt) { + if (this.yt && this.yt.ready) { this.yt.loadVideoById(data.id, data.currentTime); this.qualityRaceCondition = true; if (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() { + this.yt.ready = true; return this.setVolume(VOLUME); }; @@ -223,26 +226,26 @@ YouTubePlayer.prototype.play = function() { this.paused = false; - if (this.yt) { + if (this.yt && this.yt.ready) { return this.yt.playVideo(); } }; YouTubePlayer.prototype.pause = function() { this.paused = true; - if (this.yt) { + if (this.yt && this.yt.ready) { return this.yt.pauseVideo(); } }; YouTubePlayer.prototype.seekTo = function(time) { - if (this.yt) { + if (this.yt && this.yt.ready) { return this.yt.seekTo(time, true); } }; YouTubePlayer.prototype.setVolume = function(volume) { - if (this.yt) { + if (this.yt && this.yt.ready) { if (volume > 0) { this.yt.unMute(); } @@ -251,7 +254,7 @@ }; YouTubePlayer.prototype.getTime = function(cb) { - if (this.yt) { + if (this.yt && this.yt.ready) { return cb(this.yt.getCurrentTime()); } else { return cb(0); @@ -259,7 +262,7 @@ }; YouTubePlayer.prototype.getVolume = function(cb) { - if (this.yt) { + if (this.yt && this.yt.ready) { if (this.yt.isMuted()) { return cb(0); } else { @@ -282,14 +285,12 @@ window.loadMediaPlayer = function(data) { var e; if (data.type in TYPE_MAP) { - console.log(data); try { - window.PLAYER = TYPE_MAP[data.type](data); + return window.PLAYER = TYPE_MAP[data.type](data); } catch (_error) { e = _error; - console.error(e); + return console.error(e); } - return console.log(window.PLAYER); } }; @@ -308,12 +309,13 @@ PLAYER.play(); } if (waiting) { + PLAYER.seekTo(0); if (PLAYER instanceof YouTubePlayer) { PLAYER.pauseSeekRaceCondition = true; } else { - PLAYER.seekTo(0); PLAYER.pause(); } + return; } else if (PLAYER instanceof YouTubePlayer) { PLAYER.pauseSeekRaceCondition = false; }