无法从阵列调用视频

时间:2019-12-13 07:01:54

标签: javascript video-streaming html5-video

我正在开发一个分支视频应用程序,但在分支区域中的代码遇到了麻烦。这是AddMyListener函数的一个片段...

    function myAddListener() {
  if (new Date() - startTime > (60 * 60 * 1000)) {
    return; 
  }
  if (currentIndex == 0) {
    currentIndex = (currentIndex + 1) % videoSources.length;
    var myVideo = document.getElementsByTagName('video')[0];
    myVideo.src = videoSources[currentIndex];
    myVideo.addEventListener('ended', myNewSrc, false);
  }
  if (currentIndex == 1 && eX == true) {
    currentIndex = (currentIndex + 1) % videoSources.length;
    var myVideo = document.getElementsByTagName('video')[0];
    myVideo.src = videoSources[currentIndex];
    myVideo.addEventListener('ended', myNewSrc, false);
    console.log(currentIndex);
  }  
  if (currentIndex == 1 && eX == false){
    currentIndex = (currentIndex + 2) % videoSources.length;
    var myVideo = document.getElementsByTagName('video')[0];
    myVideo.src = videoSources[currentIndex];
    myVideo.addEventListener('ended', myNewSrc, false);
    console.log(currentIndex);
  }
  if (currentIndex == 3){
    currentIndex = (currentIndex + 1) % videoSources.length;
    var myVideo = document.getElementsByTagName('video')[0];
    myVideo.src = videoSources[currentIndex];
    myVideo.addEventListener('ended', myNewSrc, false);
    console.log(currentIndex);
  }
  if (currentIndex == 4){
    currentIndex = (currentIndex+1) % videoSources.length;
    var myVideo = document.getElementsByTagName('video')[0];
    myVideo.src = videoSources[currentIndex];
    myVideo.addEventListener('ended', myNewSrc, false);
    console.log(currentIndex);
  }
}

从CurrentIndex 0 => 1开始,一切都很好。但是,如果我们运行“ false”功能,而不是先播放+2,而是播放适当的视频,然后继续-它将跳过+ 2,+ 1,+ 1。

有人可以鸣叫吗?

1 个答案:

答案 0 :(得分:0)

我建议您使用switch语句,而不要使用多个ifs,这也将帮助您跳过。 您可以使用

您可以将index个数字放在cases中,例如case: 1等。 然后在案例中添加一个if条件来处理ex === true / false,这样就可以

switch(index) {
case 1: 
    if(ex === true) {
        // Do something
    } else if(ex === false) {
        // Do Something
    }
}