Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoplay video when slide opens #322

Closed
mikepqr opened this issue Apr 27, 2016 · 8 comments
Closed

Autoplay video when slide opens #322

mikepqr opened this issue Apr 27, 2016 · 8 comments

Comments

@mikepqr
Copy link

mikepqr commented Apr 27, 2016

I'm currently embedding webm videos with code like

<video loop autoplay controls>
    <source src="video.webm">
</video>

These videos begin autoplaying when the presentation loads. Is there any way to have them begin autoplaying when the slide loads?

This is possible in reveal but I couldn't find similar syntax in the remark documentation.

@unode
Copy link

unode commented Oct 14, 2016

Possible workaround

@nolanlawson
Copy link

I'm using the following code, which autoplays all videos from start time 0 when the slide is shown. Works well for me!

  var slideElements

  function getElementForSlide(slide) {
    slideElements = slideElements || document.querySelectorAll('.remark-slide')
    return slideElements[slide.getSlideIndex()]
  }

  slideshow.on('showSlide', function (slide) {
    Array.from(getElementForSlide(slide).querySelectorAll('video')).forEach(function (vid) {
      vid.loop = true
      vid.currentTime = 0
      vid.play()
    })
  })

  slideshow.on('hideSlide', function (slide) {
    Array.from(getElementForSlide(slide).querySelectorAll('video')).forEach(function (vid) {
      vid.pause()
    })
  })

@unode
Copy link

unode commented Dec 8, 2016

Thanks, this is much lighter than the workaround I linked.

This should also be applicable to <audio> tags.
As far as I know they use the same attributes (.loop, .currentTime, .play, .pause, ... ).

.querySelectorAll('video, audio')

seems to do the job.

@Nerdyneer
Copy link

@nolanlawson
Where should I add this code?

@abelards
Copy link
Collaborator

The question is answered on both links so I'm closing the issue.

@freder
Copy link

freder commented Mar 28, 2021

slightly improved version of @nolanlawson's solution:

  • data attribute as autoplay flag
  • mute videos in presenter mode
let slideElements;
const body = document.body;
function getElementForSlide(slide) {
	slideElements = slideElements || document.querySelectorAll('.remark-slide');
	return slideElements[slide.getSlideIndex()];
}
slideshow.on('showSlide', (slide) => {
	getElementForSlide(slide).querySelectorAll('video').forEach((vid) => {
		if (body.classList.contains('remark-presenter-mode')) {
			vid.muted = true;
		}
		if (vid.attributes['data-autoplay'].value === 'true') {
			vid.play();
		}
	});
});
slideshow.on('hideSlide', (slide) => {
	getElementForSlide(slide).querySelectorAll('video').forEach((vid) => {
		vid.pause();
	});
});

@tjex
Copy link

tjex commented Oct 20, 2023

works great 👆
just be sure to not keep autoplay="false" in any video elements, otherwise it breaks the functionality of the script.

e.g. don't do this: <video width="100%" autoplay="false" controls>

@tjex
Copy link

tjex commented Oct 20, 2023

@nolanlawson Where should I add this code?

anywhere between <script></script> tags in your main presentation's html file i.e index.html

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Remark</title>
    <link rel="stylesheet" type="text/css" href="./reflex.css" />
    <script>
      <!-- code goes here -->
    </script>
  </head>
  <body>
    <textarea id="source">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants