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





相关推荐
码农超哥同学10 分钟前
Python知识点:在Python编程中,如何使用Gensim进行主题建模
开发语言·python·面试·编程
奔跑吧邓邓子13 分钟前
JSON 全知全解:深入探索 JSON 的奥秘
开发语言·python·json
陈序缘1 小时前
Go语言实现长连接并发框架 - 消息
linux·服务器·开发语言·后端·golang
Ambition_LAO1 小时前
不同版本的 Selenium 和 WebDriver 的 API 兼容性问题
开发语言·python
worxfr1 小时前
Python Selenium常用语法汇总(包含XPath语法)
开发语言·python·selenium·xpath
weixin_545032311 小时前
JavaScript代码如何测试?
开发语言·javascript·ecmascript
谢尔登1 小时前
【React】事件机制
前端·javascript·react.js
7yewh2 小时前
C语言刷题 LeetCode 30天挑战 (八)快慢指针法
linux·c语言·开发语言·数据结构·算法·leetcode·链表
桃之夭夭ღ2 小时前
做一个不断更新的链接库
开发语言
shinelord明2 小时前
【Python】Python知识总结浅析
开发语言·人工智能·python