0

I have an array of videos and I would like to load them into the set video player that is currently on the page. Here's what I have:

var current = 0;

var videos = ["01", "02", "03", "04"];

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;

}

function shuffleAll(){
    shuffle(videos);
}

function loadVideo(){
    var video = document.getElementById('video');
    var mp4 = document.getElementById('mp4');
    d = new Date();

    mp4.src = "videos/" + videos[current] + '.mp4';
    alert(mp4.src);
    video.load();
    video.play();

}

And my HTML:

<body onLoad="shuffleAll()">

<a href="" onClick="javascript:loadVideo();">Load Video</a><br>
<video id="video" controls width="560">
     <source id="mp4" type="video/mp4" />
</video>
</body>

But I click my Load Video Button, and it doesn't do anything. What am I missing?

1 Answer 1

1

You need to pause the video then load the video then play it. It's a tricky son o bitch really. You'll need to add the listener to a videobutton (changing source via randomizing or something)

videobutton.addEventListener("click", function(event) {
   video.pause();
   mp4.setAttribute('src', 'videos/' + videos[current] + '.mp4');
   video.load();
   video.play();
},false);

This will work.

var current = 0;

var videos = ["01", "02", "03", "04"];

function shuffle(array) {
 var currentIndex = array.length, temporaryValue, randomIndex ;
 // While there remain elements to shuffle...
 while (0 !== currentIndex) {
 // Pick a remaining element...
 randomIndex = Math.floor(Math.random() * currentIndex);
 currentIndex -= 1;
 // And swap it with the current element.
 temporaryValue = array[currentIndex];
 array[currentIndex] = array[randomIndex];
 array[randomIndex] = temporaryValue;
}

 return array;

}

function shuffleAll(){
   shuffle(videos);
} 

function loadVideo(){
   var video = document.getElementById('video');
   var mp4 = document.getElementById('mp4');
   d = new Date();
   video.pause();
   mp4.setAttribute('src', 'videos/' + videos[current] + '.mp4');
   video.load();
   video.play();
 }
Sign up to request clarification or add additional context in comments.

3 Comments

No it's a cached object. so say your button is <div id="next">NEXT</div> so our videobutton would be `var videobutton = document.getElementById('next'); I'll fix it to work with yours
I'm going to need jQuery for this, huh.
No never -_- if jQuery can do it, JavaScript can do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.