【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> 
三、案例:





相关推荐
ai产品老杨15 小时前
打通各大芯片厂商相互间的壁垒,省去繁琐重复的适配流程的智慧工业开源了
人工智能·开源·音视频·能源
Yang-Never16 小时前
Kotlin协程 -> Job.join() 完整流程图与核心源码分析
android·开发语言·kotlin·android studio
牧羊狼的狼16 小时前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc
知识分享小能手18 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
luckys.one18 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
TomCode先生18 小时前
c#动态树形表达式详解
开发语言·c#
mCell18 小时前
JavaScript 的多线程能力:Worker
前端·javascript·浏览器
高-老师19 小时前
基于R语言的物种气候生态位动态量化与分布特征模拟
开发语言·r语言·物种气候
大翻哥哥19 小时前
Python 2025:量化金融与智能交易的新纪元
开发语言·python·金融
weixin_4378309420 小时前
使用冰狐智能辅助实现图形列表自动点击:OCR与HID技术详解
开发语言·javascript·ocr