多个视频一直播放,比较混乱
如图所示
代码如下:
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>
这样就可以实现,同一时间,在页面上只播放一个视频。