I am trying to add all my source video paths in the array called sources. Then, I am trying to play them using my HTML5 video player. However, when I run the loop, (debugging using alert message) - ONLY the last video plays every time, eventhough it loops through all the video names. Why is this?!
<script>
var i=1;
var sources = new Array(4);
var oggVid = document.getElementById('oggSource');
var mp4Vid = document.getElementById('mp4Source');
var webmVid = document.getElementById('webmSource');
var player = document.getElementById('videoPlayer');
for (i=1; i < sources.length; i++) {
sources[i]="/videolibrary/"+i+".webm";
var srcName=sources[i];
// alert(i + " " + sources.length + " " + srcName);
}
function next() {
player.pause();
for (i=1; i < sources.length; i++) {
webmVid.setAttribute('src', sources[i]);
mp4Vid.setAttribute('src', sources[i]);
oggVid.setAttribute('src', sources[i]);
player.load();
player.play();
alert(i + " " + sources.length + " " + sources[i]);
}} </script>
The output of alert message (when I click the Next button (that loads the next function)) shows all the sources[i] values in quick succession and the player only plays the last value every time. I don't understand why this is happening! Thanks for your help.
You don't want to loop through all videos every time you click next. Instead you only want to advance to the next video. Try this: