简体   繁体   English

HTML5:在数组的视频标签中依次播放视频吗?

[英]HTML5: Play videos one after the other in a video tag from an array?

I have been trying to play a few videos in HTML5 video tag one after the other. 我一直试图一个接一个地播放HTML5视频标签中的一些视频。

I came across a few similar questions and tried to write my code based on the given answers to those questions. 我遇到了一些类似的问题,并尝试根据给定的答案回答这些问题。

However, I can only play the first video from the array and it keeps looping the first video ! 但是,我只能播放阵列中的第一个视频 ,并且会不断循环播放第一个视频

So, This is my code: 所以,这是我的代码:

I store the videos in an array like this: 我将视频存储在这样的数组中:

videoSource = ["vidoe1.mp4", "video3.mp4", "video44.mp4", "video55.mp4", "video9.mp4"]

I try to start the videos in a click event like so: 我尝试在点击事件中开始播放视频,如下所示:

videoSource = ["vidoe1.mp4", "video3.mp4", "video44.mp4", "video55.mp4", "video9.mp4"]

$(document).on('click','.playBtn', function(e){

var videoCount = videoSource.length;
var video_index     = 0;
var video_player    = null;

function onload(){
            videosToPlay = document.getElementById("videosToPlay");
            video_player = document.getElementById("idle_video");
            video_player.setAttribute("src", videoSource[video_index]);
            videosToPlay.play();
        }

function onVideoEnded(){    
            if(video_index < videoSource.length - 1){
                video_index++;
            }
            else{
                video_index = 0;
            }
            video_player.setAttribute("src", videoSource[video_index]);
            videosToPlay.play();
        }


onload();

$("#videosToPlay").bind("ended", function() {
    //alert('Video ended!');
    onVideoEnded();
});



});

And this is my video tag: 这是我的视频标签:

<video id="videosToPlay" width="100%" preload="none" controls playsinline>

<source id="idle_video" src="" type="video/mp4">

</video>

Could someone please advice on this issue? 有人可以就这个问题提出建议吗?

Thanks in advance. 提前致谢。

This should work: 这应该工作:

videoSource = ["video1.mp4", "video3.mp4", "video44.mp4", "video55.mp4", "video9.mp4"];

$('.playBtn').click(function(e){

var videoCount = videoSource.length;
var video_index     = 0;

function onload(){
            videosToPlay = document.getElementById("videosToPlay");
            videosToPlay.addEventListener('ended',onVideoEnded,false);
            videosToPlay.src=videoSource[video_index];
            videosToPlay.play();
        }

function onVideoEnded(){    
            video_index++;
            if (video_index > videoCount-1) video_index = 0;
            videosToPlay.src=videoSource[video_index];
            videosToPlay.play();
        }


onload();

});

Also delete source element in video tag. 还要删除视频标签中的源元素。

Check it and answer if it worked :) 检查它并回答是否可行:)

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

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