javascript - 当第一个视频以视频标签作为播放列表结束时播放下一个视频
问题描述
嗨,我不太擅长 javascript
我有视频,我想在第一个视频结束时播放下一个视频等等。这是我尝试过的。
<div id="myvideo" style="width: 100%; height: 400px; overflow: hidden;">
<%= video_tag "men-kicking-punching-fighting-during-combat-sport-karate-simulation_b8ltmb7kqx_1080__D.mp4", muted: 'muted',autoplay: :true ,style: 'display: block; height: 100%; width: 100%; object-fit: cover;', class: "active"%>
<%= video_tag "young-woman-dancing-on-the-stairs_hsua5f2_1080__D.mp4", muted: 'muted',autoplay: :true,style: 'display: block; height: 100%; width: 100%; object-fit: cover;'%>
<%= video_tag "videoblocks-happy-arabian-uae-sheikh-man-walks-in-middle-of-white-desert-and-enjoys-life-on-hot-summer-day_rbtvafcvw_1080__D.mp4", muted: 'muted',autoplay: :true,style: 'display: block; height: 100%; width: 100%; object-fit: cover;' %>
</div>
在java脚本中
<script type="text/javascript">
var myvid = document.getElementById('myvideo');
myvid.addEventListener('ended', function(e) {
// get the active source and the next video source.
// I set it so if there's no next, it loops to the first one
var activevideo = document.querySelector("#myvideo video.active");
var nextvideo = document.querySelector("#myvideo video.active + video_tag") || document.querySelector("#myvideo video:first-child");
// deactivate current source, and activate next one
activevideo.className = "";
nextvideo.className = "active";
// update the video source and play
myvid.srcObject = nextvideo.srcObject;
myvid.play();
});</script>
解决方案
当一个人完成3 个视频时,要自动播放您的视频 1 by1删除自动播放,以便您的 JS 正常工作:
<script type="text/javascript">
const videos = document.getElementsByTagName('video')
videos[0].play()
videos[0].addEventListener('ended',function(e){
videos[1].play()
videos[1].addEventListener('ended',function(e){
videos[2].play()
videos[2].addEventListener('ended',function(e){
videos[0].play()
})
})
})
</script>
推荐阅读
- shell - 是否可以使用pycharm执行一段用文本文件编写的命令?
- python - PyGame将屏幕移动到另一个文件?
- javascript - 将选定的过滤器与 URL 参数同步
- sql - 当我希望员工不低于 18 岁且不高于 60 岁时,为什么会出现此错误?
- python - django logging - 无法将日志打印到文件
- javascript - 在 JavaScript 中的 For 循环中计算对象的属性数
- c# - 从 ASP.NET Core 5.0 网站上传位图到 MongoDB
- python - 用 html 文件实现 css 不起作用
- c# - JWT 令牌未在指定时间内过期
- algorithm - 使用 dp 具有特定条件的可能数组的数量