移动端项目中有个学习考试模块,要求就是视频不能快进,可以全屏,并且看完视频才能考试。
本来想着挺简单的 ,设置一下video标签的
controls controlsList="nodownload noplaybackrate " disablepictureinpicture
把右下角的三个点隐藏,里面包含下载和画中画,最新版谷歌浏览器还新增了播放速度按钮。
然后设置一下时间,拖动进度条就让他回去,然后监听视频播放结束事件,再播放结束的时候发送请求。基本思路就是这样的。
万万没想到的是,这样设置,再移动端不生效,虽然拖动进度条能回去,但是只要把进度条拖到最后,还是会触发ended
myVideo.addEventListener('ended', function () {
clearInterval(app.time)
//形成记录
let eiInfo = new EiInfo();
eiInfo.set("idNumber", $("#inqu_status-0-userId").val());
eiInfo.set("vedioId", app.id);
eiInfo.set("flag", "end");
EiCommunicator.send("PMET51V", "insVedioLog", eiInfo, {
onSuccess: function (ei) {
if (ei.getStatus() < 0) {
IPLAT.alert(ei.getMsg());
return false;
} else {
IPLAT.alert(ei.getMsg());
}
}, onFail: function (ei) {
IPLAT.alert(ei.getMsg());
}
})
});
这个问题困扰了好几天,最后找到一个timeupdate事件,就是视频播放会一直触发,然后我只需要再当前播放时间等于视频总时长时候发个请求就好了,以下是完整代码
//视频播放结束
myVideo.addEventListener("timeupdate",function(e){
console.log(myVideo.currentTime)//当前播放时间
console.log(myVideo.duration)//视频总时长
if(myVideo.currentTime==myVideo.duration){
发送请求
}
})
让进度条回去的是以下代码
var last = +app.lastPlayTime;//后台返回上次播放时间,默认是0
myVideo.ontimeupdate = function () {
var current = myVideo.currentTime;
if (current - last > 0.5) {//此处是限制跳过几秒 可调整
myVideo.currentTime = last;
} else {
last = current;
}
};
设置0.5的原因是,之前设置的隐藏倍速只能再谷歌生效,在移动端不生效,设置成0.5的话就不能两倍速播放了,如果有大佬知道怎么更好的解决这个问题,请留言