let video = uni.createVideoContext("video"+index, this) 核心代码
    video.play();

1、inde.vue 代码

<template>
	<view class="content">
		<Videobox :video="video"></Videobox>
	</view>
</template>

<script>
	import Videobox from '@/components/video/index.vue'
	export default {
		components:{ Videobox },
		data() {
			return {
				video:[{
					url:'https://www.w3school.com.cn/i/movie.mp4',
				},
				{
					url:'https://www.w3school.com.cn/i/movie.mp4',
				},
				{
					url:'https://www.w3school.com.cn/i/movie.mp4',
				}
				]
			}
		},
		onLoad() {
			
		},
		methods: {
			
		}
	}
</script>

<style lang="scss" scoped>

</style>

2、组件代码 components/video/index.vue

<template>
	<view>
		<view class="video-box" v-for="(item,index) in video" :key="index">
			<video class="video" :id="'video'+index" @play="videoPlay(index)" :src="item.url" controls object-fit="cover"></video>
		</view>
	</view>
</template>

<script>
	export default{
		name:"Videobox",
		props:{
			"video":{ type:Array },
		},
		data(){
			return{
			}
		},
		
		onShow(){
		
		},
		
		methods:{
			
			videoPlay(index){
				let video = uni.createVideoContext("video"+index, this)
				video.play();
				for(var i = 0; i< this.video.length; i++){
					if(i!=index){
						let video2 = uni.createVideoContext("video"+i,this);
						video2.pause();
					}
				}
			}
			
		}
				
	}
</script>


<style lang="scss" scoped>	
	image{width:100%; height:100%;}	
	.video-box{ height: 400rpx; position: relative; margin-bottom:20rpx;		
		.video{width: 100%;height: 100%;}
	}
</style>

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐