当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Looping through videos in HTML5

问题描述:

I have this JS code:

video_count =1;

videoPlayer = document.getElementById("homevideo");

function run(){

video_count++;

if (video_count == 4) video_count = 1;

var nextVideo = "app/video"+video_count+".mp4";

videoPlayer.src = nextVideo;

videoPlayer.play();

};

and i have two videos i want to play one after another. The videos are playing fine - first one and after ended the second one but this code loop through the second video instead of playing the first video again and after ended the second video etc.

Can you please help?

Thanks in advance,

Yaniv

网友答案:

Use the ended event to trigger the run function again.

video_count =1;
videoPlayer = document.getElementById("homevideo");
function run(){
  if (video_count == 4) video_count = 1;
  var nextVideo = "app/video"+video_count+".mp4";
  videoPlayer.src = nextVideo;
  videoPlayer.play();
  video_count++;
};
videoPlayer.addEventListener('ended', run);
网友答案:
video_count =1;
videoPlayer =     document.getElementById("homevideo");

function run(){
    if (video_count > 2) video_count = 1;
    var nextVideo = "app/video"+video_count+".mp4";
    videoPlayer.src = nextVideo;
    videoPlayer.play();
    video_count++;
};

will solve the problem You increase the variable before you define the video's file name, so you begin with two... the condition needs to be changed as well, because we wish to set videocount to 1 only after we display the 2nd video

分享给朋友:
您可能感兴趣的文章:
随机阅读: