@@ -1098,6 +1098,7 @@ function sepiaFW_build_ui_cards(){
10981098 cardBody . appendChild ( webPlayerDiv ) ;
10991099 //YouTube
11001100 } else if ( data . brand == "YouTube" ) {
1101+ //console.log('YouTube'); //DEBUG
11011102 var webPlayerDiv = document . createElement ( 'DIV' ) ;
11021103 var playerId = currentLinkItemId ++ ;
11031104 webPlayerDiv . className = "youTubeWebPlayer cardBodyItem fullWidthItem"
@@ -1109,6 +1110,7 @@ function sepiaFW_build_ui_cards(){
11091110 f . style . width = "100%" ; f . style . height = "280px" ; f . style . overflow = "hidden" ;
11101111 f . style . border = "4px solid" ; f . style . borderColor = "#212121" ;
11111112 if ( data . autoplay ) {
1113+ //console.log('add controls'); //DEBUG
11121114 if ( isSafeSource ) {
11131115 //stop all previous audio first
11141116 if ( SepiaFW . client . controls ) {
@@ -1682,11 +1684,13 @@ function sepiaFW_build_ui_cards(){
16821684 var youTubeLastActivePlayerState = undefined ;
16831685 var youTubePlayConfirmTimer = undefined ;
16841686 var youTubePlayerIsOnHold = false ;
1687+ var youTubePlayerStopRequested = false ;
16851688 var youTubePlayerAutoPlay = true ;
16861689
16871690 function addYouTubeControls ( frameEle , startWhenReady ) {
16881691 //reset some stuff first
16891692 youTubePlayerIsOnHold = false ;
1693+ youTubePlayerStopRequested = false ;
16901694 //add interface
16911695 frameEle . onload = function ( ) {
16921696 //API
@@ -1696,46 +1700,49 @@ function sepiaFW_build_ui_cards(){
16961700 } else {
16971701 youTubeMessageListenerExists = true ;
16981702 youTubePlayerAutoPlay = startWhenReady ;
1699- window . addEventListener ( 'message' , function ( e ) {
1700- if ( e . origin == "https://www.youtube.com" && e . data && typeof e . data == "string" && e . data . indexOf ( "{" ) == 0 ) {
1701- var data = JSON . parse ( e . data ) ;
1702- if ( data && data . id ) {
1703- var $player = $ ( '#' + data . id ) ;
1704- //console.log("YouTube iframe event: " + data.event);
1705- if ( $player . length == 0 ) {
1706- return ;
1707- }
1708- if ( data . event == 'onReady' ) {
1709- if ( youTubePlayerAutoPlay ) {
1710- setTimeout ( function ( ) {
1711- $player [ 0 ] . contentWindow . postMessage ( JSON . stringify ( { event :'command' , func :'playVideo' } ) , "*" ) ;
1712- } , 1000 ) ;
1713- }
1714- } else if ( data . event == 'infoDelivery' && data . info ) {
1715- //console.log(JSON.stringify(data));
1716- if ( data . info . playerState != undefined ) {
1717- youTubeLastActivePlayerState = data . info . playerState ;
1718- }
1719- youTubeLastActivePlayerId = data . id ;
1720- if ( data . info . playerState == - 1 ) {
1721- //Skip if faulty
1722- if ( youTubePlayerAutoPlay ) {
1723- youTubeSkipIfNotPlayed ( data , $player ) ; //TODO: this is tricky, we should reactivate this when user presses play
1724- }
1725- } else if ( data . info . playerState == 1 ) {
1726- clearTimeout ( youtubeSkipTimer ) ;
1727- clearTimeout ( youTubePlayConfirmTimer ) ;
1728- }
1729- }
1730- }
1731- }
1732- } ) ;
1703+ window . addEventListener ( 'message' , youTubeEventListener ) ;
17331704 }
17341705 frameEle . contentWindow . postMessage ( JSON . stringify ( { event :'listening' , id : frameEle . id } ) , "*" ) ;
17351706 } ;
17361707 //set/update fade listener
17371708 youTubeRegisterFadeInOutListener ( ) ;
17381709 }
1710+ function youTubeEventListener ( e ) {
1711+ if ( e . origin == "https://www.youtube.com" && e . data && typeof e . data == "string" && e . data . indexOf ( "{" ) == 0 ) {
1712+ var data = JSON . parse ( e . data ) ;
1713+ if ( data && data . id ) {
1714+ var $player = $ ( '#' + data . id ) ;
1715+ //console.log("YouTube iframe event: " + data.event);
1716+ if ( $player . length == 0 ) {
1717+ return ;
1718+ }
1719+ if ( data . event == 'onReady' ) {
1720+ SepiaFW . debug . info ( "Cards - YouTube: player ready." ) ;
1721+ if ( youTubePlayerAutoPlay ) {
1722+ setTimeout ( function ( ) {
1723+ Cards . youTubePlayerControls ( "resume" , $player [ 0 ] . id ) ;
1724+ } , 1000 ) ;
1725+ }
1726+ } else if ( data . event == 'infoDelivery' && data . info ) {
1727+ //console.log(JSON.stringify(data));
1728+ if ( data . info . playerState != undefined ) {
1729+ SepiaFW . debug . info ( "Cards - YouTube: player state: " + data . info . playerState ) ;
1730+ youTubeLastActivePlayerState = data . info . playerState ;
1731+ }
1732+ youTubeLastActivePlayerId = data . id ;
1733+ if ( data . info . playerState == - 1 ) {
1734+ //Skip if faulty
1735+ if ( youTubePlayerAutoPlay ) {
1736+ youTubeSkipIfNotPlayed ( data , $player ) ; //TODO: this is tricky, we should reactivate this when user presses play
1737+ }
1738+ } else if ( data . info . playerState == 1 ) {
1739+ clearTimeout ( youtubeSkipTimer ) ;
1740+ clearTimeout ( youTubePlayConfirmTimer ) ;
1741+ }
1742+ }
1743+ }
1744+ }
1745+ }
17391746 var youtubeSkipTimer = undefined ;
17401747 function youTubeSkipIfNotPlayed ( data , $player , skipFirstTest ) {
17411748 if ( skipFirstTest || data . info . availableQualityLevels . length == 0 ) {
@@ -1746,14 +1753,14 @@ function sepiaFW_build_ui_cards(){
17461753 //console.log('--- next A ---');
17471754 clearTimeout ( youtubeSkipTimer ) ;
17481755 youtubeSkipTimer = setTimeout ( function ( ) {
1749- $player [ 0 ] . contentWindow . postMessage ( JSON . stringify ( { event : 'command' , func : 'nextVideo' } ) , "*" ) ;
1756+ Cards . youTubePlayerControls ( "next" , $player [ 0 ] . id ) ;
17501757 } , 1000 ) ;
17511758 } else if ( data . info . playlist && data . info . playlist . length > 0 ) {
17521759 if ( data . info . playlistIndex != undefined && data . info . playlistIndex < ( data . info . playlist . length - 1 ) ) {
17531760 //console.log('--- next B ---');
17541761 clearTimeout ( youtubeSkipTimer ) ;
17551762 youtubeSkipTimer = setTimeout ( function ( ) {
1756- $player [ 0 ] . contentWindow . postMessage ( JSON . stringify ( { event : 'command' , func : 'nextVideo' } ) , "*" ) ;
1763+ Cards . youTubePlayerControls ( "next" , $player [ 0 ] . id ) ;
17571764 } , 1000 ) ;
17581765 delete youTubePlayersTriedToStart [ data . id ] ;
17591766 }
@@ -1790,20 +1797,24 @@ function sepiaFW_build_ui_cards(){
17901797 Cards . youTubePlayerIsOnHold = function ( ) {
17911798 return youTubePlayerIsOnHold ;
17921799 }
1793- Cards . youTubePlayerControls = function ( cmd ) {
1800+ Cards . youTubePlayerControls = function ( cmd , playerId ) {
17941801 //reset some stuff first
17951802 youTubePlayerIsOnHold = false ; //NOTE: we assume any interaction with the player resets this to false
1803+ youTubePlayerStopRequested = false ;
17961804 clearTimeout ( youTubePlayConfirmTimer ) ;
17971805
1798- if ( youTubeLastActivePlayerId && youTubeLastActivePlayerState > 0 ) {
1799- var $player = $ ( '#' + youTubeLastActivePlayerId ) ;
1806+ var playerState = ( playerId ) ? 2 : Cards . youTubePlayerGetState ( ) ; //NOTE: since we cannot read states of arbitrary players we need to assume 2 here
1807+ if ( playerId || ( youTubeLastActivePlayerId && youTubeLastActivePlayerState > 0 ) ) {
1808+ var $player = ( playerId ) ? $ ( '#' + playerId ) : $ ( '#' + youTubeLastActivePlayerId ) ;
18001809 if ( $player . length == 0 ) {
18011810 return 0 ;
18021811 } else {
1812+ SepiaFW . debug . info ( "Cards - YouTube: sending command: " + cmd ) ;
18031813 if ( cmd == "stop" || cmd == "pause" ) {
18041814 $player [ 0 ] . contentWindow . postMessage ( JSON . stringify ( { event :'command' , func :'pauseVideo' } ) , "*" ) ; //NOTE: we use pause for now because stop triggers next video
1815+ youTubePlayerStopRequested = true ;
18051816 return 1 ;
1806- } else if ( cmd == "resume" && Cards . youTubePlayerGetState ( ) == 2 ) {
1817+ } else if ( cmd == "resume" && playerState == 2 ) {
18071818 $player [ 0 ] . contentWindow . postMessage ( JSON . stringify ( { event :'command' , func :'playVideo' } ) , "*" ) ;
18081819 return 1 ;
18091820 } else if ( cmd == "next" ) {
@@ -1827,7 +1838,7 @@ function sepiaFW_build_ui_cards(){
18271838 } ,
18281839 onFadeOutRequest : function ( force ) {
18291840 //TODO: check if player is playing
1830- if ( Cards . youTubePlayerGetState ( ) == 1 ) {
1841+ if ( Cards . youTubePlayerGetState ( ) == 1 && ! youTubePlayerStopRequested ) {
18311842 Cards . youTubePlayerControls ( "pause" ) ;
18321843 youTubePlayerIsOnHold = true ;
18331844 return true ;
0 commit comments