From ed4a8b51c594fbbad0759b21677484911f077d51 Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Sat, 27 Apr 2013 18:32:56 -0400 Subject: auto play/pause youtube video when entering/leaving slide (#388) --- js/reveal.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'js/reveal.js') diff --git a/js/reveal.js b/js/reveal.js index d321dde..82a0741 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -1430,11 +1430,19 @@ var Reveal = (function(){ function startEmbeddedContent( slide ) { if( slide ) { + // HTML5 media elements toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) { if( !el.hasAttribute( 'data-ignore' ) ) { el.play(); } } ); + + // YouTube embeds + toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) { + if( !el.hasAttribute( 'data-ignore' ) ) { + el.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*'); + } + }); } } @@ -1446,11 +1454,19 @@ var Reveal = (function(){ function stopEmbeddedContent( slide ) { if( slide ) { + // HTML5 media elements toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) { if( !el.hasAttribute( 'data-ignore' ) ) { el.pause(); } } ); + + // YouTube embeds + toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) { + if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) { + el.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*'); + } + }); } } -- cgit v1.2.3