Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling the next item in my array

right now I have an array of videos. How do I make it so when i click next and prev the next or previous video in the array loads.

 <video id="video" controls autoplay width="1000">
  <source src="videos/test.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  <source src="videos/test.ogv" />
 </video>

<a href="#" onClick="javascript:vidSwap(vidURL[i+]); return false;">next</a>



<script>
var vidURL=["videos/test.ogv","videos/test2.ogv","videos/test3.ogv","videos/test4.ogv","videos/test5.ogv"    ]; // literal array
function vidSwap(vidURL) {
var myVideo = document.getElementsByTagName('video')[0];
myVideo.src = vidURL;
myVideo.load();
 myVideo.play();
}

like image 838
goetzs Avatar asked Nov 17 '11 17:11

goetzs


1 Answers

Using yout code, it'll be something like this. What you need to do is have the video that you loaded on a javascript variable. Then, when you click prev or next you can call a function that will put the correct video number and call it.

<script>
var vidURL=["videos/test.ogv","videos/test2.ogv","videos/test3.ogv","videos"]
var video = 0;

function vidSwap() {
  var myVideo = document.getElementsByTagName('video')[video];
  myVideo.src = vidURL[video];
  myVideo.load();
  myVideo.play();
}


function prevVideo() {
  if(video == 0) {
    video = vidUrl.length;
  }
  else {
    video -= 1;
  }

  vidSwap();
}

function nextVideo() {
  if(video == length) {
    video = 0;
  }
  else {
    video += 1;
  }

  vidSwap();
}

</script>


 <video id="video" controls autoplay width="1000">
  <source src="videos/test.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
  <source src="videos/test.ogv" />
 </video>

<a href="#" onClick="javascript:prevVideo(); return false;">prev</a>
<a href="#" onClick="javascript:nextVideo(); return false;">next</a>
like image 65
aF. Avatar answered Sep 30 '22 02:09

aF.