Remove automatic JavaScript script play

0

Good evening, everyone. I found a code here in the stack for random audio playlist and adapted it so I could run videos, however I'm new to Javascript and I do not know how to do almost anything. I even need a tutorial all of a sudden. More complete, of course, because those I attended did not help me much.

The code below is the playlist code I need. The only problem is that I'm going to run the playlist on a Reveal.js slide and use the autoplaymedia = true setting. This option allows the video to only load when the slide with the video is displayed. However, in order to work, javascript can not load the video automatically but Reveal.js. I basically need to be removed from the script below the option to automatically play, as this is what is being done.

/* Objects */
var _player = document.getElementById('player');

/* Aplication */
var tracks = [


'http://media.w3.org/2010/05/sintel/trailer.mp4',

function playNext() {
  var track = tracks[Math.floor(Math.random() * tracks.length)];

  _player.src = track;
  return _player.play();
}

/* Events */
window.addEventListener('load', playNext);
_player.addEventListener('ended', playNext);

Link to codepen (based on Caio Rate code): Link to codepen

    
asked by anonymous 18.05.2017 / 02:23

1 answer

0

In this case, removing autoplay is simple.

Just remove return _player.play(); from your script.

However, you will not be able to play the video if you do not put the controls parameter in the video div

Script

/* Objects */
var _player = document.getElementById('player');

/* Aplication */
var tracks = [
  'http://media.w3.org/2010/05/sintel/trailer.mp4',
  'http://mirror.cessen.com/blender.org/peach/trailer/trailer_iphone.m4v',
  'https://ia800209.us.archive.org/20/items/ElephantsDream/ed_1024.mp4',
];

function playNext() {
  var track = tracks[Math.floor(Math.random() * tracks.length)];

  _player.src = track;

}

/* Events */
window.addEventListener('load', playNext);
_player.addEventListener('ended', playNext);
<video id="player" controls>
  O seu navegador não suporta vídeo em HTML5.
</video>
    
18.05.2017 / 15:35