hakimel / reveal.js

The HTML Presentation Framework
https://revealjs.com
MIT License
67.94k stars 16.65k forks source link

Youtube play/pause #2698

Open stopoverandcatchfire opened 4 years ago

stopoverandcatchfire commented 4 years ago

I have 4 sections with 4 youtube videos inside. When uploading, all videos are paused and only the video in the first section automatically plays with video and audio. When the slide is changed, the next video goes into play but the previous one is stopped instead of being paused.

<section><iframe width="800" height="600" data-src="https://www.youtube.com/embed/XFkzRNyygfk"></iframe></section>
<section><iframe width="800" height="600" data-src="https://www.youtube.com/embed/vpRPnhuz78U"></iframe></section>
<section><iframe width="800" height="600" data-src="https://www.youtube.com/embed/v3xwCkhmies"></iframe></section>
<section><iframe width="800" height="600" data-src="https://player.vimeo.com/video/155035278"></iframe></section>

Checking with the browser debug (Firefox) I noticed that the tag "?enablejsapi=1" is added for Youtube and "?api=1" for Vimeo.

Play is activated, then the following code is executed:

// YouTube postMessage API
if( /youtube\.com\/embed\//.test( iframe.getAttribute( 'src' ) ) && autoplay ) {
    iframe.contentWindow.postMessage( '{"event":"command","func":"playVideo","args":""}', '*' );
}
// Vimeo postMessage API
else if( /player\.vimeo\.com\//.test( iframe.getAttribute( 'src' ) ) && autoplay ) {
    iframe.contentWindow.postMessage( '{"method":"play"}', '*' );
}

But when changing splides the "pause" command is not executed:

// YouTube postMessage API
queryAll( element, 'iframe[src*="youtube.com/embed/"]' ).forEach( el => {
    if( !el.hasAttribute( 'data-ignore' ) && el.contentWindow && typeof el.contentWindow.postMessage === 'function' ) {
        el.contentWindow.postMessage( '{"event":"command","func":"pauseVideo","args":""}', '*' );
    }
});

// Vimeo postMessage API
queryAll( element, 'iframe[src*="player.vimeo.com/"]' ).forEach( el => {
    if( !el.hasAttribute( 'data-ignore' ) && el.contentWindow && typeof el.contentWindow.postMessage === 'function' ) {
        el.contentWindow.postMessage( '{"method":"pause"}', '*' );
    }
});

What is missing to pause videos instead of being stopped ?

Hermen17 commented 4 years ago

É só não alterar o slide