gpt4 book ai didi

javascript - HTML5 : Play videos one after the other in a video tag from an array?

转载 作者:行者123 更新时间:2023-12-03 02:53:23 25 4
gpt4 key购买 nike

我一直在尝试在 HTML5 视频标签中逐个播放一些视频。

我遇到了一些类似的问题,并尝试根据这些问题的给定答案编写我的代码。

但是,我只能播放数组中的第一个视频,并且它会一直循环第一个视频!

所以,这是我的代码:

我将视频存储在如下数组中:

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

我尝试在点击事件中启动视频,如下所示:

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();
});



});

这是我的视频标签:

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

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

</video>

有人可以就这个问题提出建议吗?

提前致谢。

最佳答案

这应该有效:

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();

});

同时删除视频标签中的源元素。

检查并回答是否有效:)

关于javascript - HTML5 : Play videos one after the other in a video tag from an array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47741771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com