Automatic Javascript Script Remove Play

Asked

Viewed 206 times

0

Good night, you guys. I found a code here in the stack for random audio playlist and adapted it so that I could run videos, but I’m new to Javascript and I don’t know how to do almost anything. I even need a tutorial suddenly. More complete, of course, because the ones I watched didn’t help me much.

The code below is the playlist code I need. The only problem is that I will run the playlist on a Reveal.js slide and used the autoplaymedia=true configuration. This option allows the video to only load when the slide with the video is displayed. However, javascript cannot load the video automatically but Reveal.js. I need to basically be removed from the script below the option to play automatically as that 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 the code of Caio Tarifa): Link to codepen

  • 1

    the video has no controls, remove automatic playback as will play later?

1 answer

0

In your case to remove automatic playback is simple.

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

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

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.