使用VideoContext解决多个视频同时播放的问题

前情提要

组件:video

小程序里要放置视频,用video组件。video组件很多属性,如:

  • src,string类型,表示视频资源的地址。
  • bindplay,当开始继续播放时触发play事件。

API:wx.createVideoContext()创建VideoContext实例

VideoContext 实例,可调用 wx.createVideoContext() 创建。
VideoContext实例通过 id 与一个video组件绑定,便可操作对应的video组件。
VideoContext实例包含如下方法:

  • play(),播放视频。
  • stop(),停止视频。

API:wx.showLoading()

wx.showLoading(Object object),显示loading提示框,调用 wx.hideLoading() 才能关闭提示框。
wx.showLoading()接收一个对象作为参数,该对象包含如下属性:

  • title,提示的内容,值是字符串类型,必填。
  • mask,是否显示透明蒙层,布尔值,默认是false,即默认不显示透明蒙层。
  • success,接口调用成功时的回调函数。
  • fail,接口调用失败时的回调函数。
  • complete,接口调用结束(不论接口调用成功或失败)时的回调函数。

搭建视频服务器

  1. 全局安装serve:npm install -g serve
  2. 在任何你想的地方新建文件夹:resources。resources下新建文件夹:images,用于存放静态图片资源;resources下新建文件夹:videos,用于存放视频文件。
  3. 启动服务器:serve resources
    在这里插入图片描述

小程序项目

代码涉及的文件有:

  1. app.json
  2. pages/index/index.wxml
  3. pages/index/index.wxss
  4. pages/index/index.js

app.json

{
  "pages": [
    "pages/index/index"
  ],
  "window": {
    "navigationBarBackgroundColor": "#0149af",
    "navigationBarTitleText": "首页",
    "navigationBarTextStyle": "white"
  },
  "style": "v2",
  "sitemapLocation": "sitemap.json"
}

pages/index/index.wxml

<view class="cameraContainer">
  <view class="header">
    <input type="text" class="search" placeholder="搜一搜 这里啥都有"/>
    <image src="/static/images/search.png"></image>
  </view>
  <view class="tabContainer">
    <scroll-view class="tabs" enable-flex scroll-x>
      <view class="tab {{item.id===tabId?'active':''}}" wx:for="{{tabList}}" wx:key="id" 
            id="{{item.id}}" bindtap="changeTab">
        {{item.name}}
      </view>
    </scroll-view>
  </view>
  <scroll-view class="videoList" scroll-y>
      <view class="videoItem" wx:for="{{videoList}}" wx:key="vid">
        <video src="{{item.videoUrl}}" id="{{item.vid}}"></video>
      </view>
    </scroll-view>
</view>

pages/index/index.wxss

.cameraContainer{
  padding: 10rpx;
}
.header{
  display: flex;
  align-items: center;
  border: 1rpx solid #aaa;
  border-radius: 6rpx;
  padding: 6rpx 10rpx;
}
.header image{
  width: 36rpx;
  height: 36rpx;
  padding: 0 10rpx;
}
.header .search{
  flex: 1;
  height: 36rpx;
  line-height: 36rpx;
  font-size: 26rpx;
}
.tabContainer{
  margin-top: 20rpx;
}
.tabs{
  display: flex;
  height: 50rpx;
}
.tab{
  height: 40rpx;
  line-height: 40rpx;
  margin-right: 30rpx;
  white-space: nowrap;
  font-size: 28rpx;
}
.active{
  border-bottom: 4rpx solid #0149af;
}
.videoItem{
  margin-top: 10rpx;
}
.videoItem video{
  width: 100%;
  border-radius: 10rpx;
}

pages/index/index.js

Page({
  data:{
    tabList:[],
    tabId:'',
    videoList:[]
  },
  onLoad(){
    this.getTabList();
    this.getVideoList(1);
  },
  changeTab(e){
    // 切换tab时,因为请求视频数据可能耗时较长,故给出信息,提示正在加载
    wx.showLoading({
      title: '正在加载'
    })
    const tabId = e.target.id;
    this.setData({
      tabId,
      videoList:[]  //在新的视频数据返回前,将videoList置空,以免页面呈现旧的视频数据
    });

    if(tabId === "1") this.getVideoList(1);
    else if(tabId === "2") this.getVideoList(2);
    else this.getVideoList(2);
  },
  getTabList(){
    let result = [
      {"id": "1","name": "现场"},
      {"id": "2","name": "广告"},
      {"id": "3","name": "舞蹈"},
      {"id": "4","name": "MV",},
      {"id": "5","name": "生活"},
      {"id": "6","name": "游戏"},
      {"id": "7","name": "演唱会",},
      {"id": "8","name": "粤语现场"},
      {"id": "9","name": "华语现场"},
      {"id": "10","name": "欧美现场"},
      {"id": "11","name": "流行现场"},
      {"id": "12","name": "民谣现场"}
    ]
    this.setData({
      tabList:result,
      tabId:result.length && result[0].id || ''
    })
  },
  getVideoList(flag){
    const result1 = [
      {vid: "D4FD37BC59E440F367E485680D172FFC", coverUrl: "http://localhost:3000/images/1.jpg", videoUrl: "http://localhost:3000/videos/1.mp4"},
      {vid: "28DCBBE25A686078549195E54B5F1612", coverUrl: "http://localhost:3000/images/2.jpg", videoUrl: "http://localhost:3000/videos/2.mp4"},
      {vid: "C5B8699EBF70B139B702841EA41E7C4B", coverUrl: "http://localhost:3000/images/3.jpg", videoUrl: "http://localhost:3000/videos/3.mp4"},
      {vid: "77C5143F298F169DDD0DD3EF116187E9", coverUrl: "http://localhost:3000/images/4.jpg", videoUrl: "http://localhost:3000/videos/4.mp4"},
    ];
    const result2 = [
      {vid: "1C612837556283740A191AD4FF774F2C", coverUrl: "http://localhost:3000/images/5.jpg", videoUrl: "http://localhost:3000/videos/5.mp4"},
      {vid: "8EB59877C11DF774B68C7EA6F2F46574", coverUrl: "http://localhost:3000/images/6.jpg", videoUrl: "http://localhost:3000/videos/6.mp4"},
      {vid: "F50333C85BC61F7DBC4EE65ED481747E", coverUrl: "http://localhost:3000/images/7.jpg", videoUrl: "http://localhost:3000/videos/7.mp4"},
      {vid: "0C6590B104C45A471AF29C1758DC36E0", coverUrl: "http://localhost:3000/images/8.jpg", videoUrl: "http://localhost:3000/videos/8.mp4"}
    ]
    //用定时器模拟响应耗时
    setTimeout(() => {
      wx.hideLoading();
      if(flag === 1) this.setData({videoList:result1});
      else if(flag === 2) this.setData({videoList:result2});
    },500);
  }
})

问题描述

在播放一个视频的时候,可以播放另一个视频,存在多个视频同时播放的问题。
在这里插入图片描述

解决方法

解决思路:在播放另一个视频的时候,将关闭上一个视频,保证同一时间内只有一个视频在播放。
解决措施:代码变更涉及的文件有

  1. pages/index/index.html,video组件添加bindplay属性。
  2. pages/index/index.js,定义bindplay的事件处理函数handlePlay

在这里插入图片描述

pages/index/index.html

<view class="cameraContainer">
  <view class="header">
    <input type="text" class="search" placeholder="搜一搜 这里啥都有"/>
    <image src="/static/images/search.png"></image>
  </view>
  <view class="tabContainer">
    <scroll-view class="tabs" enable-flex scroll-x>
      <view class="tab {{item.id===tabId?'active':''}}" wx:for="{{tabList}}" wx:key="id" 
            id="{{item.id}}" bindtap="changeTab">
        {{item.name}}
      </view>
    </scroll-view>
  </view>
  <scroll-view class="videoList" scroll-y>
      <view class="videoItem" wx:for="{{videoList}}" wx:key="vid">
        <video src="{{item.videoUrl}}" id="{{item.vid}}" bindplay="handlePlay"></video>
      </view>
    </scroll-view>
</view>

pages/index/index.js

Page({
  data:{
    tabList:[],
    tabId:'',
    videoList:[]
  },
  onLoad(){
    this.getTabList();
    this.getVideoList(1);
  },
  changeTab(e){
    // 切换tab时,因为请求视频数据可能耗时较长,故给出信息,提示正在加载
    wx.showLoading({
      title: '正在加载'
    })
    const tabId = e.target.id;
    this.setData({
      tabId,
      videoList:[]  //在新的视频数据返回前,将videoList置空,以免页面呈现旧的视频数据
    });

    if(tabId === "1") this.getVideoList(1);
    else if(tabId === "2") this.getVideoList(2);
    else this.getVideoList(2);
  },
  getTabList(){
    let result = [
      {"id": "1","name": "现场"},
      {"id": "2","name": "广告"},
      {"id": "3","name": "舞蹈"},
      {"id": "4","name": "MV",},
      {"id": "5","name": "生活"},
      {"id": "6","name": "游戏"},
      {"id": "7","name": "演唱会",},
      {"id": "8","name": "粤语现场"},
      {"id": "9","name": "华语现场"},
      {"id": "10","name": "欧美现场"},
      {"id": "11","name": "流行现场"},
      {"id": "12","name": "民谣现场"}
    ]
    this.setData({
      tabList:result,
      tabId:result.length && result[0].id || ''
    })
  },
  getVideoList(flag){
    const result1 = [
      {vid: "D4FD37BC59E440F367E485680D172FFC", coverUrl: "http://localhost:3000/images/1.jpg", videoUrl: "http://localhost:3000/videos/1.mp4"},
      {vid: "28DCBBE25A686078549195E54B5F1612", coverUrl: "http://localhost:3000/images/2.jpg", videoUrl: "http://localhost:3000/videos/2.mp4"},
      {vid: "C5B8699EBF70B139B702841EA41E7C4B", coverUrl: "http://localhost:3000/images/3.jpg", videoUrl: "http://localhost:3000/videos/3.mp4"},
      {vid: "77C5143F298F169DDD0DD3EF116187E9", coverUrl: "http://localhost:3000/images/4.jpg", videoUrl: "http://localhost:3000/videos/4.mp4"},
    ];
    const result2 = [
      {vid: "1C612837556283740A191AD4FF774F2C", coverUrl: "http://localhost:3000/images/5.jpg", videoUrl: "http://localhost:3000/videos/5.mp4"},
      {vid: "8EB59877C11DF774B68C7EA6F2F46574", coverUrl: "http://localhost:3000/images/6.jpg", videoUrl: "http://localhost:3000/videos/6.mp4"},
      {vid: "F50333C85BC61F7DBC4EE65ED481747E", coverUrl: "http://localhost:3000/images/7.jpg", videoUrl: "http://localhost:3000/videos/7.mp4"},
      {vid: "0C6590B104C45A471AF29C1758DC36E0", coverUrl: "http://localhost:3000/images/8.jpg", videoUrl: "http://localhost:3000/videos/8.mp4"}
    ]
    //用定时器模拟响应耗时
    setTimeout(() => {
      wx.hideLoading();
      if(flag === 1) this.setData({videoList:result1});
      else if(flag === 2) this.setData({videoList:result2});
    },500);
  },
  handlePlay(event){
    let vid = event.target.id;
    //bindtap,播放或继续播放时触发play事件。
    //播放下一个视频时,关闭上一个视频,但继续播放原视频除外。
    this.videoContext && this.vid!==vid && this.videoContext.stop();

    this.vid = vid;
    this.videoContext = wx.createVideoContext(vid);   
  }
})

相关链接

微信小程序中使用video组件
使用scroll-view实现tabs
使用微信小程序的video组件

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值