uni-app阻止多个视频一起播放的解决

多个视频一直播放,比较混乱

如图所示
在这里插入图片描述
代码如下:
template:

<view class="imgCard" v-if="item.video!=null">
	<video class="imgOrVideo"  :data-id="index.toString()" @play="playVideo" :data-url="imageAddress+item.video" :src="imageAddress+item.video" @click.stop='nothing()' controls :show-center-play-btn="false" auto-pause-if-navigate></video>
</view>

js:

//视频播放
			playVideo(e){
				console.log(e)
				let currentId=e.currentTarget.dataset.id;
				let url=e.currentTarget.dataset.url
				this.videoContent = uni.createVideoContext(currentId);
				this.videoList.push({//这写法是有问题的,再次暂停播放,会重复添加,需要去重,侧重点也不是在这,忽略
					"id":currentId,
					"url":url
				});
				let trailer = this.videoList;
				trailer.forEach(function(item,index){
					if(item.url!=null && item.url!=""){
						let temp = item.id;
						if(temp != currentId){
							console.log("暂停其他");
							console.log(temp);
							console.log(uni.createVideoContext(temp,this));
							uni.createVideoContext(temp,this).pause();
						}
					}
				})
			}

这样去运行,uni.createVideoContext(temp,this).pause();一直在控制台打印undefined,排了好些时间,突然想起,创建这个VideoContext是通过什么去选取节点呢?仔细看了一下template里的代码,发现少了id,于是在加上:id=“index.toString()”,因为这个id需要一个String类型的,用toString()转换,代码如下,只需改动template的代码,如下

<view class="imgCard" v-if="item.video!=null">
	<video class="imgOrVideo"  :data-id="index.toString()" @play="playVideo" :data-url="imageAddress+item.video" :src="imageAddress+item.video" @click.stop='nothing()' controls :show-center-play-btn="false" auto-pause-if-navigate></video>
</view>

这样就可以实现,同一时间,在页面上只播放一个视频。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值