微信小程序实现只能播放一个视频,其他视频暂停

原理:1.点击当前视频,先判断是否有视频正在播放,如果没有,立即播放,如果有,则暂停其他视频,再播放当前视频

话不多说,直接看代码

js

data: {
	list:[1,2,3,4],
    name:'',
    test:'test',
    tag: false,
    left:0,
    indexCurrent: null,
  },

methods: {
  video_play(e) {
      var curIdx = e.currentTarget.id;
      // 没有播放时播放视频
      // console.log(curIdx)
      if (!this.data.indexCurrent) {
        this.setData({
          indexCurrent: curIdx
        })
        var videoContext = wx.createVideoContext(curIdx,this) //这里对应的视频id
        videoContext.play()
      } else { // 有播放时先将prev暂停,再播放当前点击的current
        var videoContextPrev = wx.createVideoContext(this.data.indexCurrent,this)//this是在自定义组件下,当前组件实例的this,以操作组件内 video 组件(在自定义组件中药加上this,如果是普通页面即不需要加)
        if (this.data.indexCurrent != curIdx) {
          console.log(123)
          videoContextPrev.pause()
          this.setData({
            indexCurrent: curIdx
          })
          var videoContextCurrent = wx.createVideoContext(curIdx,this)
          videoContextCurrent.play()
        }
      }
    },
}

wxss

<view class="main" wx:for="list" wx:for-index="index" wx:key="index" wx:for-index="index">
	<view class="video" >
    	<video src="http://1253438777.vod2.myqcloud.com/e21e1ff6vodtransgzp1253438777/b237ff415285890793696679612/v.f20.mp4" class="video_video" show-fullscreen-btn id="video{{index}}" bindtap="video_play"></video>
	</view>
</view>

在这里css样式就不写了,防止扰乱自己,

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值