小程序 列表视频 及 滑过该视频 停止播放

分析原因:因为有个列表页,包含图片+文字类,文字类,视频类。视频类占比比较多。正常使用wx:for进行循环展示,在且换tab的时候导致页面卡顿,调查发现,当页面全是图片或者文字的时候,不会出现这种情况,只有视频比较多的时候会出现,开发者工具也会经常黑屏,需要重新打开

定位问题 :多视频加载导致

**解决问题1 **:在所有的视频上添加一张图片做封面

<view wx:for-item="teach" wx:key="index">
	<view class="vodeoList">
		<view>
			<video autoplay class="myVideo" id="video{{index}}" src="{{item.attachment}}" wx:if="{{playingIndex == index}></video>
		</view>
		<view id="{{index}}" class="{{playingIndex == index ? 'noneClass cover' : 'blockClass cover' }}" bindtap="videoPlay">
			<image class="myImg" src="{{item.cover!= '' ? item.cover : '../../image/vio.png'}}"></image>
			<image class="playImg" src="../../image/play.png"></image>
		</view>
	</view>
</view>
.noneClass{
 display: none;
}
.blockClass{
 display: block;
}
.vodeoList{
  position: relative;
}
.playImg{
  position: absolute;
  left: calc(50% - 28rpx);
  top: calc(50% - 28rpx);
  z-index: 99;
  width: 56rpx;
  height: 56rpx;
}
.myVideo,.myImg{
  height: 350rpx;
  width: 100%;
  margin-top: 15rpx;
}
.myImg{
  border: 1rpx solid #f1f1f1;
  background: #333;
}
videoPlay: function (e) {
   var id = e.currentTarget.id
   if (!this.data.playingIndex) {
     this.setData({
       playingIndex: id
     })
     var videoContext = wx.createVideoContext('video' + this.data.playingIndex)
     videoContext.play()
   } else {
     var videoContextPre = wx.createVideoContext('video' + this.data.playingIndex)
     videoContextPre.seek(0)
     videoContextPre.pause()
     this.setData({
       playingIndex: id
     })
     var videoContext = wx.createVideoContext('video' + this.data.playingIndex)
     videoContext.play()
   }
 },

解决问题2 : 页面滚动,当前视频停止播放。需要写onPageScroll方法

// 滚动监听生命周期函数(视频播放暂停)
  onPageScroll(e) { 
    let screenHeight = wx.getSystemInfoSync().windowHeight
    let topBottomPadding = (screenHeight - 175) / 2
    const videoObserve = wx.createIntersectionObserver()
    videoObserve.relativeToViewport({
        bottom: -topBottomPadding,
        top: -topBottomPadding
      })
      .observe(".myVideo", (res) => {
      var videoContext = wx.createVideoContext(res.id)
        let {
          intersectionRatio
        } = res
        if (intersectionRatio === 0) {
          //离开视界,因为视窗占比为0,停止播放
          videoContext.pause()
        }
      })
  },

onPageScroll引发的BUG
视频正常的停止按钮不好用了(画面停住了,但是声音并没有停止),由于没有真机测试,直接提交了代码,真机测试的时候发现,停止按钮不好用了

onPageScroll引发的BUG – 解决办法:给video标签添加停止事件,代码来控制暂停 bindpause="stopPlay"

<video autoplay bindpause="stopPlay" class="myVideo" id="video{{ index }}" 
src="{{item..attachment}}" wx:if="{{playingIndex == index}}"></video>
stopPlay(e) {
 var id = e.currentTarget.id
 var videoContextPre = wx.createVideoContext('index' + this.data.playingIndex)
 videoContextPre.pause()
},
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值