Skip to content

Commit 360bc94

Browse files
committed
new fix for prematurely autoplaying background videos
1 parent 9f4ed56 commit 360bc94

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

js/reveal.js

+30-37
Original file line numberDiff line numberDiff line change
@@ -3054,6 +3054,13 @@
30543054

30553055
}
30563056

3057+
/**
3058+
* Called when the given slide is within the configured view
3059+
* distance. Shows the slide element and loads any content
3060+
* that is set to load lazily (data-src).
3061+
*
3062+
* @param {HTMLElement} slide Slide to show
3063+
*/
30573064
/**
30583065
* Called when the given slide is within the configured view
30593066
* distance. Shows the slide element and loads any content
@@ -3098,6 +3105,7 @@
30983105

30993106
// If the background contains media, load it
31003107
if( background.hasAttribute( 'data-loaded' ) === false ) {
3108+
background.setAttribute( 'data-loaded', 'true' );
31013109

31023110
var backgroundImage = slide.getAttribute( 'data-background-image' ),
31033111
backgroundVideo = slide.getAttribute( 'data-background-video' ),
@@ -3107,43 +3115,38 @@
31073115

31083116
// Images
31093117
if( backgroundImage ) {
3110-
31113118
background.style.backgroundImage = 'url('+ backgroundImage +')';
3112-
background.setAttribute( 'data-loaded', 'true' );
3113-
31143119
}
31153120
// Videos
31163121
else if ( backgroundVideo && !isSpeakerNotes() ) {
3122+
var video = document.createElement( 'video' );
31173123

3118-
if( !isOverview() ) {
3119-
3120-
var video = document.createElement( 'video' );
3121-
video.setAttribute( 'autoplay', '' );
3122-
video.setAttribute( 'playsinline', '' );
3123-
3124-
if( backgroundVideoLoop ) {
3125-
video.setAttribute( 'loop', '' );
3126-
}
3127-
3128-
if( backgroundVideoMuted ) {
3129-
video.muted = true;
3130-
}
3131-
3132-
// Support comma separated lists of video sources
3133-
backgroundVideo.split( ',' ).forEach( function( source ) {
3134-
video.innerHTML += '<source src="'+ source +'">';
3135-
} );
3136-
3137-
background.appendChild( video );
3124+
if( backgroundVideoLoop ) {
3125+
video.setAttribute( 'loop', '' );
3126+
}
31383127

3139-
background.setAttribute( 'data-loaded', 'true' );
3128+
if( backgroundVideoMuted ) {
3129+
video.muted = true;
3130+
}
31403131

3132+
// Inline video playback works (at least in Mobile Safari) as
3133+
// long as the video is muted and the `playsinline` attribute is
3134+
// present
3135+
if( isMobileDevice ) {
3136+
video.muted = true;
3137+
video.autoplay = true;
3138+
video.setAttribute( 'playsinline', '' );
31413139
}
31423140

3141+
// Support comma separated lists of video sources
3142+
backgroundVideo.split( ',' ).forEach( function( source ) {
3143+
video.innerHTML += '<source src="'+ source +'">';
3144+
} );
3145+
3146+
background.appendChild( video );
31433147
}
31443148
// Iframes
31453149
else if( backgroundIframe ) {
3146-
31473150
var iframe = document.createElement( 'iframe' );
31483151
iframe.setAttribute( 'allowfullscreen', '' );
31493152
iframe.setAttribute( 'mozallowfullscreen', '' );
@@ -3164,17 +3167,9 @@
31643167
iframe.style.maxWidth = '100%';
31653168

31663169
background.appendChild( iframe );
3167-
3168-
background.setAttribute( 'data-loaded', 'true' );
3169-
31703170
}
3171-
else {
3172-
3173-
background.setAttribute( 'data-loaded', 'true' );
3174-
3175-
}
3176-
31773171
}
3172+
31783173
}
31793174

31803175
}
@@ -3415,10 +3410,8 @@
34153410
* the targeted slide.
34163411
*
34173412
* @param {HTMLElement} element
3418-
* @param {boolean} autoplay Optionally override the
3419-
* autoplay setting of media elements
34203413
*/
3421-
function stopEmbeddedContent( element, autoplay ) {
3414+
function stopEmbeddedContent( element ) {
34223415

34233416
if( element && element.parentNode ) {
34243417
// HTML5 media elements

0 commit comments

Comments
 (0)