简体   繁体   English

html5视频播放器播放列表循环

[英]html5 video player playlist loop

This question might be asked before. 可能会问这个问题。 I have an array of videos. 我有很多视频。 What I want to loop this playlist when it's end. 结束时我想循环播放的列表。

My code works fine and it loop through all the video one by one and play but the problem is when it plays last video after this it stop. 我的代码运行良好,并且可以循环播放所有视频,然后播放,但问题是在停止播放最后一个视频时。 I want to start over again from first video. 我想从第一个视频重新开始。 I will appreciate any help. 我将不胜感激。

Here is the code 这是代码

var videoList = [..]

var curVideo = 0;
var videoPlayer = document.getElementById('videoPlayer');
videoPlayer.onended = function(){
        curVideo++;
    if(curVideo < videoList.length){            
        videoPlayer.src = videoList[curVideo];        
    } else if (curVideo == videoList.length){
        console.log("list is finished");        
    }
}

Change your if statement to while and then when curVideo reaches the end, set curVideo back to 0. That should create an infinite loop. if语句更改为while ,然后在curVideo到达末尾时,将curVideo设置回0。这将创建一个无限循环。

You would of course need to change your else if to an if to check to see if it's at the end. 当然,您需要将else if更改为if以检查它是否在末尾。

It works for me. 这个对我有用。 I figured it out. 我想到了。 This is the code maybe someone can benefit from it. 这是代码,也许有人可以从中受益。

var videoList = [..]
var curVideo = 0;
var videoPlayer = document.getElementById('videoPlayer');
videoPlayer.onended = function(){
    curVideo++;  

    if(curVideo < videoList.length){            
        videoPlayer.src = videoList[curVideo];  
      //  return;      
    }  
    if (curVideo == videoList.length){
        curVideo=0;
       videoPlayer.src = videoList[curVideo];       
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM