h5video视频的无缝切换类似爱奇艺附源码下载
日期:2020-07-30
来源:程序思维浏览:2885次
h5video视频的无缝切换在网上找了好多方法都不知所以然,试了很多方法都无法做到中间不闪烁无缝切换,最终自己想到了一个方案。
原理是先将video标签overflow: hidden;(不能使用display:none;会被禁止预载播放),让第二个视频play()后在canplay事件里暂停自身,然后播放第一个视频,第一个视频播放结束事件ended里执行第二个视频的播放,以此做到无缝切换。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>video视频的无缝切换</title>
<style type="text/css">
/*视频*/
#video_box{position: fixed;left: 0;top: 0;width: 100%;height: 100%;overflow: hidden;}
#video_box video{
display: block; margin: 0 auto; width: 100%;height: 100%; /*object-fit:fill;*/
position: absolute;left: 0;top: 0;visibility: hidden;
}
</style>
</head>
<body>
<div id="video_box">
<video id="vplayer_start" muted controls src="video/1.mp4" webkit-playsinline="true" playsinline="true" x-webkit-airplay="allow" x5-video-player-type="h5" x5-video-player-fullscreen="true" x5-video-orientation="portraint" style="object-fit:fill"></video>
<video id="vplayer" muted controls src="video/4.mp4" webkit-playsinline="true" playsinline="true" x-webkit-airplay="allow" x5-video-player-type="h5" x5-video-player-fullscreen="true" x5-video-orientation="portraint" style="object-fit:fill;"></video>
</div>
<script>
var startPlay=true;
var vplayer_start = document.getElementById('vplayer_start');
var vplayer = document.getElementById('vplayer'),
video_box = document.getElementById('video_box');
vplayer_start.pause();
vplayer.play();
// 播放结束
vplayer_start.addEventListener("ended",function(e){
vplayer.style.visibility = "visible";
vplayer.play();
e.target.style.display = "none";
});
//循环播放第二个视频
vplayer.addEventListener("ended",function(e){
e.target.play();
});
vplayer.addEventListener('canplay', function(e){
if(startPlay){
e.target.pause();
vplayer_start.style.visibility = "visible";
vplayer_start.play();
startPlay=false;
}
})
</script>
</body>
</html>
h5video视频的无缝切换源码下载:链接:https://pan.baidu.com/s/1VgsFbR0ss3ZzvmDiuJ2Flg 密码:zo24
原理是先将video标签overflow: hidden;(不能使用display:none;会被禁止预载播放),让第二个视频play()后在canplay事件里暂停自身,然后播放第一个视频,第一个视频播放结束事件ended里执行第二个视频的播放,以此做到无缝切换。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>video视频的无缝切换</title>
<style type="text/css">
/*视频*/
#video_box{position: fixed;left: 0;top: 0;width: 100%;height: 100%;overflow: hidden;}
#video_box video{
display: block; margin: 0 auto; width: 100%;height: 100%; /*object-fit:fill;*/
position: absolute;left: 0;top: 0;visibility: hidden;
}
</style>
</head>
<body>
<div id="video_box">
<video id="vplayer_start" muted controls src="video/1.mp4" webkit-playsinline="true" playsinline="true" x-webkit-airplay="allow" x5-video-player-type="h5" x5-video-player-fullscreen="true" x5-video-orientation="portraint" style="object-fit:fill"></video>
<video id="vplayer" muted controls src="video/4.mp4" webkit-playsinline="true" playsinline="true" x-webkit-airplay="allow" x5-video-player-type="h5" x5-video-player-fullscreen="true" x5-video-orientation="portraint" style="object-fit:fill;"></video>
</div>
<script>
var startPlay=true;
var vplayer_start = document.getElementById('vplayer_start');
var vplayer = document.getElementById('vplayer'),
video_box = document.getElementById('video_box');
vplayer_start.pause();
vplayer.play();
// 播放结束
vplayer_start.addEventListener("ended",function(e){
vplayer.style.visibility = "visible";
vplayer.play();
e.target.style.display = "none";
});
//循环播放第二个视频
vplayer.addEventListener("ended",function(e){
e.target.play();
});
vplayer.addEventListener('canplay', function(e){
if(startPlay){
e.target.pause();
vplayer_start.style.visibility = "visible";
vplayer_start.play();
startPlay=false;
}
})
</script>
</body>
</html>
h5video视频的无缝切换源码下载:链接:https://pan.baidu.com/s/1VgsFbR0ss3ZzvmDiuJ2Flg 密码:zo24
- 上一篇:js如何操作伪类元素before,after
- 下一篇:h5video有声自动播放
精品好课