video继续播放时跳到指定位置

前情提要

组件:video

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

  • src,视频资源的地址,string类型,必填。
  • poster,视频封面的图片,string类型,非必填。
  • object-fit,当视频大小与容器大小不一直时,视频的表现形式,支持以下3个值:
    • contain,包含(默认值)
    • fill,填充
    • cover,覆盖
  • bindplay,当开始继续播放时触发play事件。
  • bindtimeupdate,播放进度变化时触发,触发频率:250ms/次。每次触发时,记录了最新的播放进度:event.detail.currentTime
  • bindended,视频播放到末尾时触发ended事件。

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

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

  • play(),播放视频。
  • stop(),停止视频。
  • seek(number position),跳转到指定位置。接受的参数是number类型,单位为s,即秒。

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}}" poster="{{item.coverUrl}}" object-fit="cover" bindplay="handlePlay" bindtimeupdate="handleTimeUpdate" bindended="handleEnded" wx:if="{{videoId === item.vid}}"></video>
      <image src="{{item.coverUrl}}" id="{{item.vid}}" bindtap="handlePlay" wx:else></image>
    </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;
}
.videoList{
  height: calc(100vh - 150rpx); 
}
.videoItem{
  margin-top: 10rpx;
}
.videoItem video{
  width: 100%;
  height: 450rpx;
  border-radius: 10rpx;
}
.videoItem image{
  width: 100%;
  height: 450rpx;
  border-radius: 10rpx;
}

pages/index/index.js

const host = "http://localhost:3000";

Page({
  data:{
    tabList:[],
    tabId:'',
    videoList:[],
    videoId:"",
    cache:[]
  },
  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: host+"/images/1.jpg", videoUrl: host+"/videos/1.mp4"},
      {vid: "28DCBBE25A686078549195E54B5F1612", coverUrl: host+"/images/2.jpg", videoUrl: host+"/videos/2.mp4"},
      {vid: "C5B8699EBF70B139B702841EA41E7C4B", coverUrl: host+"/images/3.jpg", videoUrl: host+"/videos/3.mp4"},
      {vid: "77C5143F298F169DDD0DD3EF116187E9", coverUrl: host+"/images/4.jpg", videoUrl: host+"/videos/4.mp4"},
    ];
    const result2 = [
      {vid: "1C612837556283740A191AD4FF774F2C", coverUrl: host+"/images/5.jpg", videoUrl: host+"/videos/5.mp4"},
      {vid: "8EB59877C11DF774B68C7EA6F2F46574", coverUrl: host+"/images/6.jpg", videoUrl: host+"/videos/6.mp4"},
      {vid: "F50333C85BC61F7DBC4EE65ED481747E", coverUrl: host+"/images/7.jpg", videoUrl: host+"/videos/7.mp4"},
      {vid: "0C6590B104C45A471AF29C1758DC36E0", coverUrl: host+"/images/8.jpg", videoUrl: host+"/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;
    this.setData({videoId:vid})

    this.videoContext = wx.createVideoContext(vid);

    let tempObj = this.data.cache.find(item => item.id === vid);
    //如果视频之前播放过,则此次为继续播放,因此从上次停止的位置开始播放
    if(tempObj) this.videoContext.seek(tempObj.currentTime);
    //如果视频第一次播放,则此次从头开始播放即可
    else this.videoContext.play()
  },
  handleTimeUpdate(event){
    let id = event.target.id;
    let currentTime = event.detail.currentTime;
    let cacheObj = {id,currentTime};

    let cache = this.data.cache;

    let tempObj = cache.find(item => item.id === id);
    //如果cache中已记录该video相关信息
    if(tempObj) tempObj.currentTime = currentTime;
    //如果cache中未记录该video相关信息
    else cache.push(cacheObj);

    this.setData({cache})
  },
  handleEnded(event){
    const id = event.target.id;
    const cache = this.data.cache;
    //video播放结束,将该video信息从cache中移除
    const index = cache.findIndex(item => item.id === id);
    cache.splice(index,1);

    this.setData({cache});
  }
})

相关链接

video列表性能优化
同一页面存在多个video时,video无法正常播放一直在加载转圈
使用VideoContext解决多个视频同时播放的问题
微信小程序中使用video组件
使用scroll-view实现tabs
使用微信小程序的video组件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值