需求
- 播放暂停视频
- 不允许快进,可以后退
- 视频后退不会影响最高观看时长,例如看了10分钟,后退5分钟,观看时长依然是600秒
- 监听退出记录观看时间,下次进来接着看
- 视频看完积分
<template>
<view>
<!-- id:唯一标识,@timeupdate进度条变化的事件,@ended进度条到最后的事件,initial-time指定视频初始播放位置, -->
<video id="myVideo" style="width: 100%;" @timeupdate="timeUpdate" @ended="ended" :initial-time="initialTime"
:src="course.videos" :poster="course.img">
</video>
</view>
</template>
<script>
export default {
data() {
return {
initialTime: 0,
duration: 0,
videoContext: '',
watchTime: 0,
videoRealTime: 0,
course: {
videos: "https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/%E7%AC%AC1%E8%AE%B2%EF%BC%88uni-app%E4%BA%A7%E5%93%81%E4%BB%8B%E7%BB%8D%EF%BC%89-%20DCloud%E5%AE%98%E6%96%B9%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B@20181126-lite.m4v",
img: 'https://cdn.uviewui.com/uview/swiper/swiper1.png'
}
};
},
onLoad(options) {
uni.setNavigationBarTitle({
title: options.id
})
this.watchTime = 67
this.initialTime = 67
},
onReady() {
this.videoContext = uni.createVideoContext('myVideo')
},
methods: {
timeUpdate(e) {
this.duration = parseInt(e.detail.duration)
var jumpTime = parseInt(e.detail.currentTime)
if (jumpTime - this.watchTime > 1) {
this.videoContext.seek(this.watchTime)
} else {
this.videoRealTime = parseInt(e.detail.currentTime)
if (this.videoRealTime > this.watchTime) {
this.watchTime = this.videoRealTime
}
}
},
ended() {
if (this.watchTime < this.duration) {
this.videoContext.play()
} else {
console.log('看完了')
}
},
onBackPress(e) {
console.log('返回', e, this.watchTime, this.duration);
},
},
}
</script>