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


一、效果:

image.png

二、实现:
<!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> 
三、案例:

image.png
image.png
image.png
image.png
image.png
image.png

相关推荐

  1. vue 配合 video.js 实现视频播放

    2024-01-02 10:38:01       9 阅读
  2. html页面视频标签时设定只能播放视频

    2024-01-02 10:38:01       44 阅读
  3. Vue实现视频播放

    2024-01-02 10:38:01       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-02 10:38:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-02 10:38:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-02 10:38:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-02 10:38:01       20 阅读

热门阅读

  1. mysqldump导出函数、存储过程和视图

    2024-01-02 10:38:01       46 阅读
  2. 【排序算法】LeetCode-347. 前 K 个高频元素

    2024-01-02 10:38:01       34 阅读
  3. 系统监视工具 | htop

    2024-01-02 10:38:01       35 阅读
  4. Linux Shell 021-输入输出重定向

    2024-01-02 10:38:01       39 阅读
  5. 概率论基础

    2024-01-02 10:38:01       25 阅读
  6. c基础(二)

    2024-01-02 10:38:01       36 阅读
  7. shell变量详解

    2024-01-02 10:38:01       31 阅读
  8. Android中线程间的通信-Handler

    2024-01-02 10:38:01       33 阅读
  9. 浪潮软件开发校招面试一面凉经

    2024-01-02 10:38:01       38 阅读
  10. 逻辑运算符——and和&的区别

    2024-01-02 10:38:01       35 阅读
  11. Pytest单元测试系列[v1.0.0][pytest插件常用技巧]

    2024-01-02 10:38:01       39 阅读