Remark: Autoplay video when slide opens

Created on 27 Apr 2016  ·  6Comments  ·  Source: gnab/remark

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.

Most helpful comment

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()
    })
  })

All 6 comments

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()
    })
  })

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.

@nolanlawson
Where should I add this code?

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

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();
    });
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephentu picture stephentu  ·  5Comments

fladd picture fladd  ·  7Comments

royfrancis picture royfrancis  ·  4Comments

donquixote picture donquixote  ·  5Comments

zmonoid picture zmonoid  ·  5Comments