martial-arts/pagesB/play-video/play-video.nvue

180 lines
5.8 KiB
Plaintext
Raw Permalink Normal View History

2022-08-04 09:44:27 +00:00
<template>
<div style="width:100%;overflow-x:hidden;">
<image class="" @tap="backEv" :style="{top:statusHeight+'px'}" src="/static/public/video-back.png" style="width: 33rpx;height: 33rpx;position: fixed;z-index: 10;left: 20rpx;" mode="aspectFill"></image>
2022-08-07 01:50:06 +00:00
<swiper @transition="getxy" @change="switchVideo" :current="currentIndex" vertical :style="{width:newwidth+'px',height:newHeight+'px'}">
2022-08-05 10:01:57 +00:00
<swiper-item @tap="doubleClick(index)" v-for="(item,index) in videoList" :key="index">
<!-- :poster="item.cover" -->
<video :id="`video${index}`" :ref="`refvideo_${index}`" style="width: 100%;" :style="{height:newHeight+'px'}" :autoplay="item.autoplay"
:src="item.src" :enable-play-gesture="dans"
:controls="item.isControls" :show-center-play-btn="isCenterImg" :show-play-btn="isBottomImg" :show-fullscreen-btn="isFull"
2023-03-23 02:51:22 +00:00
@play="comePlay(index)" @pause="comePause(index)" @timeupdate='videoTimeUpdateEvent' :enable-progress-gesture="isBottomImg"></video>
2022-08-04 09:44:27 +00:00
<!-- 封面图 -->
2022-08-07 01:50:06 +00:00
<image v-if="item.isPlayImg" @tap.stop="playEv(index)" src="/static/tabbar/icon-play.png" style="position: fixed;top: 50%;left: 50%;
2022-08-04 09:44:27 +00:00
width: 126rpx;height: 126rpx;transform: translate(-50%, -50%);" mode="aspectFill"></image>
</swiper-item>
</swiper>
</div>
</template>
<script>
export default {
data(){
return {
statusHeight: uni.getSystemInfoSync().statusBarHeight + 42/2 - 10,
newHeight:uni.getSystemInfoSync().windowHeight,
newwidth:uni.getSystemInfoSync().windowWidth,
videoSrc:'https://video.shipin520.com/videos/17/97/40/a_yiPIEXbE43Tm1559179740.mp4',
2022-08-05 10:01:57 +00:00
videoList:[],
currentIndex:0,
2022-08-04 09:44:27 +00:00
videoContext:'',
title:'',//视频的标题,全屏时在顶部展示
posterSrc:'',//视频封面的图片网络资源地址,如果 controls 属性值为 false 则设置 poster 无效
dans:true,//是否开启播放手势,即双击切换播放/暂停
isCenterImg:false,//是否显示视频中间的播放按钮
isBottomImg:true,//是否显示视频底部控制栏的播放按钮
2022-08-05 10:01:57 +00:00
isFull:true,//是否显示全屏按钮
2023-02-21 06:20:06 +00:00
count:0,
2023-03-23 02:51:22 +00:00
currentTime:'' ,//记录观看时长
duration:'',//视频总时长
video_real_time: 0,
nitial_time: ''
2022-08-04 09:44:27 +00:00
}
},
2022-08-05 10:01:57 +00:00
onReady: (res)=> {
2022-08-04 09:44:27 +00:00
},
2022-08-05 10:01:57 +00:00
onLoad(op) {
2022-08-10 10:18:06 +00:00
// this.currentIndex = op.current;
this.currentIndex = 0;
2022-08-07 01:50:06 +00:00
uni.getStorageSync('videoList').forEach(item=>{
let obj = {
...item,
isPlayImg:true,
autoplay:false,
isControls:false
}
this.videoList.push(obj);
})
this.videoList[this.currentIndex].autoplay = true;
2022-08-05 10:01:57 +00:00
this.videoContext = uni.createVideoContext(`video${this.currentIndex}`, this)
2022-08-04 09:44:27 +00:00
},
2022-08-07 01:50:06 +00:00
onHide() {
uni.removeStorageSync('videoList');
2023-02-21 06:20:06 +00:00
},
onUnload() {
let course_video_id = this.videoList[this.currentIndex].course_video_id;
2023-03-21 01:15:29 +00:00
console.log(course_video_id,'页面销毁了')
2023-03-16 10:09:57 +00:00
if(course_video_id) {
let params = {
course_video_id:course_video_id,
duration:parseInt(this.currentTime),
2023-02-21 06:20:06 +00:00
}
2023-03-16 10:09:57 +00:00
this.$requst.get('/api/course/create-browse-records',params).then(res=>{
if(res.code == 0) {
console.log('观影记录提交成功',this.currentTime)
}
})
}
2022-08-07 01:50:06 +00:00
},
2022-08-04 09:44:27 +00:00
methods:{
2022-08-07 01:50:06 +00:00
// 视频滑动结束
getxy(e){
let num = e.detail.dy;
if(num==this.newHeight || -num==this.newHeight){
this.playCurrentVideo(this.currentIndex);
}
},
// 滑动swiper 设置当前视频索引
switchVideo(e){
this.currentIndex = e.detail.current;
},
2022-08-05 10:01:57 +00:00
// 双击
doubleClick(index){
this.count++;
setTimeout(()=>{
2022-08-07 01:50:06 +00:00
this.count = 0;
},300)
if(this.count>2){
if(this.videoList[index].isPlayImg){
// 如果当前视频正在播放中,双击及暂停播放
this.videoContext.pause();
2022-08-05 10:01:57 +00:00
this.videoList[index].isPlayImg = true;
2022-08-07 01:50:06 +00:00
} else {
// 如果当前视频暂停中,双击及开始播放
this.videoList[index].isPlayImg = false;
this.videoContext.play();
2022-08-05 10:01:57 +00:00
}
2022-08-07 01:50:06 +00:00
}
2022-08-05 10:01:57 +00:00
},
// 播放当前视频
playCurrentVideo(index){
this.videoContext.pause();
2022-08-07 01:50:06 +00:00
this.videoContext = uni.createVideoContext(`video${index}`, this);
2022-08-05 10:01:57 +00:00
this.videoList[index].isPlayImg = false;
2022-08-07 01:50:06 +00:00
this.videoContext.play();
2022-08-05 10:01:57 +00:00
},
2022-08-07 01:50:06 +00:00
// 退出视频播放
2022-08-04 09:44:27 +00:00
backEv(){
uni.navigateBack({
delta:1
})
},
2022-08-07 01:50:06 +00:00
// 点击播放图标,播放视频
2022-08-05 10:01:57 +00:00
playEv(index){
this.playCurrentVideo(index);
2022-08-04 09:44:27 +00:00
},
2022-08-05 10:01:57 +00:00
// 视频播放
comePlay(index){//当开始/继续播放时触发play事件
this.videoList[index].isPlayImg = false;
this.videoList[index].isControls = true;
2022-08-04 09:44:27 +00:00
},
2022-08-05 10:01:57 +00:00
// 视频暂停
comePause(index){
this.videoList[index].isPlayImg = true;
this.videoList[index].isControls = false;
2023-02-21 06:20:06 +00:00
},
//记录观看时长
2023-03-23 02:51:22 +00:00
videoTimeUpdateEvent(e) {
var isReady = 1; // 是否开启可以视频快进 1 禁止开启
//跳转到指定播放位置 initial-time 时间为秒
let that = this;
//播放的总时长
var duration = e.detail.duration;
//实时播放进度 秒数
var currentTime = parseInt(e.detail.currentTime);
//当前视频进度
// console.log("视频播放到第" + currentTime + "秒")//查看正在播放时间,以秒为单位
if (that.video_real_time == 0) {
var jump_time = parseInt(that.initial_time) + parseInt(that.video_real_time);
} else {
var jump_time = parseInt(that.video_real_time);
}
if (isReady == 1) {
if (currentTime > jump_time && currentTime - jump_time > 3) {
let videoContext = wx.createVideoContext('video0');
videoContext.seek(that.video_real_time);
}
}
that.video_real_time = currentTime; //实时播放进度
that.currentTime = Math.ceil(e.detail.currentTime);
that.duration = parseInt(e.detail.duration - 1);
if(that.currentTime >= that.duration) {
setTimeout(()=>{
uni.navigateBack({
delta:1
})
},200)
}
},
},
2022-08-04 09:44:27 +00:00
}
</script>
<style>
</style>