From d8f109d036f04ff528c5a4a7060087d9ec054720 Mon Sep 17 00:00:00 2001 From: Momchil Marinov Date: Wed, 16 Mar 2016 14:45:33 +0100 Subject: [PATCH 1/2] start/stop update timer in respect to player state --- youtube.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/youtube.js b/youtube.js index c0a6e14..046298b 100644 --- a/youtube.js +++ b/youtube.js @@ -103,24 +103,36 @@ angular.module("info.vietnamcode.nampnq.videogular.plugins.youtube", []) API.mediaElement[0].pause = function () { ytplayer.pauseVideo(); }; - function updateTime() { - API.onUpdateTime({ - target: API.mediaElement[0] - }); - } - updateTimer = setInterval(updateTime, 600); + updateTime(); // Initial time update angular.element(ytplayer.getIframe()).css({'width':'100%','height':'100%'}); - + // Trigger canplay event var event = new CustomEvent("canplay"); API.mediaElement[0].dispatchEvent(event); } + function updateTime() { + API.onUpdateTime({ + target: API.mediaElement[0] + }); + } + + function startUpdateTimer(interval) { + updateTimer = setInterval(updateTime, interval); + } + + function stopUpdateTimer() { + if (updateTimer) { + clearInterval(updateTimer); + } + } + function onVideoStateChange(event) { var player = event.target; switch (event.data) { case YT.PlayerState.ENDED: + stopUpdateTimer(); API.onComplete(); break; @@ -129,6 +141,7 @@ angular.module("info.vietnamcode.nampnq.videogular.plugins.youtube", []) var event = new CustomEvent("playing"); API.mediaElement[0].dispatchEvent(event); API.setState(VG_STATES.PLAY); + startUpdateTimer(600); break; case YT.PlayerState.PAUSED: @@ -137,6 +150,7 @@ angular.module("info.vietnamcode.nampnq.videogular.plugins.youtube", []) if (API.currentState == VG_STATES.PLAY) { API.setState(VG_STATES.PAUSE); } + stopUpdateTimer(); break; case YT.PlayerState.BUFFERING: @@ -181,7 +195,7 @@ angular.module("info.vietnamcode.nampnq.videogular.plugins.youtube", []) } ); scope.$on('$destroy', function() { - clearInterval(updateTimer); + stopUpdateTimer(); }); } }; From 7671167f03115079c00278ca80ae0d5282caf0a5 Mon Sep 17 00:00:00 2001 From: Alex Wilde Date: Wed, 23 Mar 2016 20:03:24 +0100 Subject: [PATCH 2/2] Stop any already running updateTimer interval before initializing a new interval. --- youtube.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/youtube.js b/youtube.js index 046298b..59a78f9 100644 --- a/youtube.js +++ b/youtube.js @@ -118,6 +118,9 @@ angular.module("info.vietnamcode.nampnq.videogular.plugins.youtube", []) } function startUpdateTimer(interval) { + if (updateTimer) { + stopUpdateTimer(); + } updateTimer = setInterval(updateTime, interval); }