【js】js实现多个视频连续播放:

文章目录


一、效果:
二、实现:
javascript 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Video Player</title>
		<style>
        #progressBar {   
  					width: 800px;
            height: 20px;
   					background-color: #ddd;
				}
        #progress {
            height: 20px;
           	background-color: #abc;
        }
        #videoContainer {
            position: relative;
            width: 800px; 
           	height: 450px; /* adjust as needed */
        }
      	#videoContainer video {
          	position: absolute;
            width: 100%;
          	height: 100%;
        }
 		</style>
</head>
<body>
   		<div id="videoContainer"></div>
			<button id="playButton">播放</button>
			<button id="pauseButton">暂停</button>
			<div id="progressBar"><div id="progress"></div></div>
    	<div>Total duration: <span id="totalDuration">0</span> seconds</div>
      <div>Current time: <span id="currentTime">0</span> seconds</div>
      
    	<script>
      	var videoContainer = document.getElementById('videoContainer');
      	var videoSources = ["f1.mp4", "f2.mp4", "f3.mp4", "f4.mp4"];
      	var totalDuration = 0;
				var totalCurrentTime = 0;
				var currentVideo = 0;
				var videoElements = videoSources.map(function (src, i) {
            var video = document.createElement('video');
   					video.src = src;
          	video.addEventListener('loadedmetadata', function  () {
                if (i < videoSources.length - 1) {
                    videoElements[i + 1].load();
                }
            });
           video.addEventListener('play', function () {
              	totalDuration = videoElements.reduce(function (sum, video) {
                    return sum + (video.duration || 0);
                }, 0); 
             		document.getElementById('totalDuration').textContent = totalDuration;
                		totalCurrentTime = videoElements.slice(0, i).reduce(function (sum, video) {
                    return sum + (video.duration || 0);
                }, 0);
             		document.getElementById('currentTime').textContent = totalCurrentTime;
          	});
          	if (i > 0) {
                video.style.display  = 'none';
            }
            videoContainer.appendChild(video);
            return video;
        });

      	videoElements[0].load();

      	videoElements.forEach(function (videoElement, i) {
          	videoElement.addEventListener('timeupdate', function () {
                totalCurrentTime = videoElement.currentTime;
                for (var j =  0; j < i; j++) {
                    totalCurrentTime += videoElements[j].duration;
              	}
                document.getElementById('currentTime').textContent = totalCurrentTime;
        				var progress = totalCurrentTime / totalDuration * 100;
                document.getElementById('progress').style.width = progress + '%';
           	}, false);

          	videoElement.addEventListener('ended', function () {
                currentVideo++;
                if (currentVideo < videoSources.length) {
                    videoElements[currentVideo].play();
                    videoElement.style.display = 'none';
             				videoElements[currentVideo].style.display = 'block';
                }
            }, false);
        });

      	document.getElementById('playButton').addEventListener('click', function () {
            videoElements[currentVideo].play();
        }, false);

      	document.getElementById('pauseButton').addEventListener('click', function () {
            videoElements[currentVideo].pause();
        }, false);
   	</script>
</body>
</html> 
三、案例:





相关推荐
z落落8 分钟前
C# 字段与属性(get/set访问器、三种属性写法、只读属性)+属性拦截例子(get动态计算 + set数据校验)
开发语言·c#
影寂ldy17 分钟前
C#栈和队列
开发语言·c#
SilentSamsara25 分钟前
SQLAlchemy 2.x:异步 ORM 与数据库迁移 Alembic 完整指南
开发语言·数据库·python·sql·青少年编程·oracle·fastapi
basketball61631 分钟前
C++ static_cast 完全解析
开发语言·c++
子安柠40 分钟前
Go语言并发编程:协程与管道详解
开发语言·后端·golang
এ慕ོ冬℘゜42 分钟前
手写生产级 jQuery Toast 轻量提示组件|零插件依赖、动画流畅、极简高
前端·javascript·jquery
程序大视界44 分钟前
【Python系列课程】Python面向对象(下):封装、继承与多态
开发语言·python
Lumbrologist1 小时前
【C++】零基础入门 · 第 12 节:模板与 STL 入门
开发语言·c++
天月风沙1 小时前
基于机器视觉的实验室器件仓储系统设计——内蒙古自治区国家级大创工程存档
开发语言·python
24zhgjx-fuhao1 小时前
虚链路的配置
开发语言·网络·php