Jas*_*ann 5 html javascript html5-video
我知道以前有人问过类似的问题,但没有人能够解决这个具体问题,他们只能解决部分问题。
我想实现以下目标:
video1,video2,video3,video4和video5。video1完成后video2播放,然后video3,video4,video5然后再次从 开始video1。video3,那就再继续打video4,video5,video1,video2等。我已经能够编写一个代码来完成从第 1 点到第 4 点的所有操作,但我无法解决第 5 点!
这是我的代码。我已将background-color视频的设置为使视频red之间的间隙可见 - 您将能够在每个视频的播放之间看到红色闪光。这就是我需要解决的问题:我需要消除那一秒的差距,以便播放绝对无缝。
var vidElement = document.getElementById('video');
var vidSources = [
"http://www.w3schools.com/html/mov_bbb.mp4",
"http://www.w3schools.com/html/movie.mp4"
];
var activeVideo = Math.floor((Math.random() * vidSources.length));
vidElement.src = vidSources[activeVideo];
vidElement.addEventListener('ended', function(e) {
// update the active video index
activeVideo = (++activeVideo) % vidSources.length;
if(activeVideo === vidSources.length){
activeVideo = 0;
}
// update the video source and play
vidElement.src = vidSources[activeVideo];
vidElement.play();
});Run Code Online (Sandbox Code Playgroud)
video { background-color: red }Run Code Online (Sandbox Code Playgroud)
<video src="http://www.w3schools.com/html/mov_bbb.mp4" id="video" autoplay muted playsinline></video>
<p>(each video is just ~ 10 seconds)</p>Run Code Online (Sandbox Code Playgroud)
您可以创建两个video具有preload属性的元素并将其添加到 div 容器中。然后我们可以通过切换显示状态来在视频之间切换,如下所示:
var videoContainer = document.getElementById('videoContainer'),
output = document.getElementById('output'),
nextVideo,
videoObjects =
[
document.createElement('video'),
document.createElement('video')
],
vidSources =
[
"http://www.w3schools.com/html/mov_bbb.mp4",
"http://www.w3schools.com/html/movie.mp4",
"http://www.w3schools.com/html/mov_bbb.mp4",
"http://www.w3schools.com/html/movie.mp4",
"http://www.w3schools.com/html/mov_bbb.mp4",
"http://www.w3schools.com/html/movie.mp4"
//this list could be additionally filled without any other changing from code
],
//random starting point
nextActiveVideo = Math.floor((Math.random() * vidSources.length));
videoObjects[0].inx = 0; //set index
videoObjects[1].inx = 1;
initVideoElement(videoObjects[0]);
initVideoElement(videoObjects[1]);
videoObjects[0].autoplay = true;
videoObjects[0].src = vidSources[nextActiveVideo];
videoContainer.appendChild(videoObjects[0]);
videoObjects[1].style.display = 'none';
videoContainer.appendChild(videoObjects[1]);
function initVideoElement(video)
{
video.playsinline = true;
video.muted = false;
video.preload = 'auto'; //but do not set autoplay, because it deletes preload
//loadedmetadata is wrong because if we use it then we get endless loop
video.onplaying = function(e)
{
output.innerHTML = 'Current video source index: ' + nextActiveVideo;
//select next index. If is equal vidSources.length then it is 0
nextActiveVideo = ++nextActiveVideo % vidSources.length;
//replace the video elements against each other:
if(this.inx == 0)
nextVideo = videoObjects[1];
else
nextVideo = videoObjects[0];
nextVideo.src = vidSources[nextActiveVideo];
nextVideo.pause();
};
video.onended = function(e)
{
this.style.display = 'none';
nextVideo.style.display = 'block';
nextVideo.play();
};
}Run Code Online (Sandbox Code Playgroud)
video{background-color: red}Run Code Online (Sandbox Code Playgroud)
<div id="videoContainer" style="display:inline-block"></div>
<b id="output" style="vertical-align:top"></b>Run Code Online (Sandbox Code Playgroud)