swiper-vertical-video.uvue 5.6 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2 3
	<view class="page">
    <swiper class="swiper" :current="current" :circular="index != 0" :vertical="true" @change="onSwiperChange" @transition="onTransition">
M
mehaotian 已提交
4
			<swiper-item class="swiper-item" v-for="(item,i) in visibleList" :key="i">
5 6
				<video :ref="'video-'+i" class="video-box" :id="item._id" :src="item.src" :poster="item.poster_src" :show-center-play-btn="false" :autoplay="false" :controls="false" :loop="true" @play="onPlay(i)" @pause="onPause(i)"></video>
				<view class="video-cover" @click="changeState(i)">
M
mehaotian 已提交
7 8 9 10 11 12 13
					<image v-if="!state[i]" class="play-btn" src="/static/template/swiper-vertical-video/play.png" mode="widthFix"></image>
				</view>
				<view class="video-info">
					<text class="video-info-text">容器:第 {{i}} 个</text>
					<text class="video-info-text">内容:{{item.content}}</text>
				</view>
			</swiper-item>
14 15 16 17 18
		</swiper>
    <view class="debug-info">
      <text class="status-text">debug-info 播放状态:</text>
      <text class="status-text" v-for="(value,index) in state">第{{index+1}}个:{{value?'播放中':'已暂停'}}</text>
    </view>
M
mehaotian 已提交
19
	</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
20
</template>
M
mehaotian 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<script>
	import { VideoNode } from "uts.sdk.modules.DCloudUniVideo";
	type ListItem = { _id : string, content : string, src : string, poster_src : string }
	let page : number = 0;

	export default {
		components: {},
		data() {
			return {
				list: [] as ListItem[],
				visibleList: [] as ListItem[],// 提高性能 可见的只有3个
				current: 0 as number,
				index: 0,
				state: [false, false, false] as boolean[]
			}
		},
		beforeCreate() {
			this.list = this.getData()
			this.visibleList = this.list.slice(0, 3)
		},
		watch: {
			current(current : number, oldCurrent : number) {
				let changeNumber = current - oldCurrent
				if (changeNumber == 1 || changeNumber == -2) {
					// console.error('向右');
					this.index++
				} else {
					// console.error('向左');
					this.index--
				}
				// //翻页(3项为一页)
				if (Math.abs(changeNumber) == 2) {
					// console.log('翻页');
					page = Math.floor(this.index / 3);
					// console.log(this.index);
					// console.log('page',page);
					// console.log('slice',3*page,3*page+3);
					if (this.list.length < 3 * page + 3) {
						let list : ListItem[] = this.getData()
						this.list.push(...list.toTypedArray())
					}

					let visibleList = this.list.slice(3 * page, 3 * page + 3)
					// 换数据
					this.visibleList = visibleList
				}
			},
			current(current) {
				this.state.forEach((val : boolean, index : number) => {
					if (index === current) {
						this.doPlay(current)
					} else if (val) {
						// 除了选中的其他已经播放的都需要停止
						this.doPause(index)
						console.log('index:' + index + '已被执行暂停');
					}
				})
			}
		},
		onReady() {
			// 一启动完成,就播放第一个
			this.doPlay(0)
		},
84
		methods: {
M
mehaotian 已提交
85 86 87 88 89 90 91
			changeState(index : number) {
				if (this.state[index]) {
					this.doPause(index)
				} else {
					this.doPlay(current)
				}
			},
92 93
			doPlay(index : number) {
        console.log("doPlay  video-" + index,this.$refs["video-" + index]);
M
mehaotian 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
				(this.$refs["video-" + index] as VideoNode).play()
			},
			doPause(index : number) {
				(this.$refs["video-" + index] as VideoNode).pause()
				console.log("doPause  video-" + index);
			},
			onPause(index : number) {
				this.state[index] = false
				console.log('onPause', index);
			},
			onPlay(index : number) {
				this.state[index] = true
				console.log('onPlay', index);
			},
			getData() : ListItem[] {
				/* Promise 暂不支持
				Promise<void> 
				return new Promise((resolve) => {
					resolve();
				});*/

				let videoUrlList = [
116 117 118
					'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/video/uts.mp4',
					'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/video/uni-ai.mp4',
					'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/video/uni-verify.mp4'
M
mehaotian 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
				] as string[]

				let posterSrcList = [
					'/static/template/swiper-vertical-video/poster/uni-uts.jpg',
					'/static/template/swiper-vertical-video/poster/uni-ai.jpg',
					'/static/template/swiper-vertical-video/poster/uni-verify.jpg'
				] as string[]

				let list = [] as ListItem[];
				for (let i = 0; i < 6; i++) {
					let index = this.list.length + i;
					let listItem : ListItem = {
						"_id": "a00" + index,
						"content": "这是第" + index + "条数据" + videoUrlList[i % 3],
						"src": videoUrlList[i % 3],
						"poster_src": posterSrcList[i % 3]
					}
					list.push(listItem)
				}
				return list
			},
			onSwiperChange(e : SwiperChangeEvent) {
				// console.error('SwiperChangeEvent',e.detail.current);
				this.current = e.detail.current;
			},
			onTransition(/*e : SwiperTransitionEvent*/) {
				// console.log('onTransition e.detail.dx', e.detail.dx);
			}
		}
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
149 150 151
</script>

<style>
M
mehaotian 已提交
152
	.page {
153 154 155 156
    width: 750rpx;
    flex: 1;
	}
  
M
mehaotian 已提交
157 158
	.swiper,
	.swiper-item,
159
	.video-box,
M
mehaotian 已提交
160
	.video-cover {
161 162 163
		width:750rpx;
    flex: 1;
    height: 100%;
M
mehaotian 已提交
164 165 166 167 168 169
	}

	.swiper-item {
		position: relative;
	}

170 171 172
	.video-box {
    width: 750rpx;
  }
M
mehaotian 已提交
173

174 175
	.video-cover {
    position: absolute;
M
mehaotian 已提交
176 177
		justify-content: center;
		align-items: center;
178 179 180 181 182 183 184 185
		align-content: center;
	}
  
  .play-btn {
  	width: 30px;
  	height: 30px;
  	color: #FFF;
  }
M
mehaotian 已提交
186 187

	.video-info {
188 189 190
    position: absolute;
    bottom: 0;
    padding: 15px;
M
mehaotian 已提交
191 192 193 194
	}

	.video-info-text {
		font-size: 14px;
195
		color: red;
M
mehaotian 已提交
196 197 198
		line-height: 20px;
	}

199 200 201 202 203 204 205 206 207
  .debug-info{
    position: fixed;
    top:15px;
    width: 750rpx;
  }
  .status-text{
    color:red;
    padding: 15px;
  }
208
</style>