uniapp视频音频从上次断开位置开始播放


uniapp-H5视频从上次断开位置开始播放

<video
  :src="video.sourceVideoUrl"
  :poster="video.coverUrl"
  controls
  @timeupdate="timeupdate"
  id="demoVideo"
>
</video>
<script>
  methods: {
    //判断是否安卓设备
    isAndroid(){
      var u = navigator.userAgent;
      if(u.indexOf('Android') > -1 || u.indexOf('Linux') > -1){
        return true;
      }
    },
    // 播放进度变化时触发事件
    timeupdate(e){
      // console.log(e)
      this.currentTime = Math.floor(e.detail.currentTime);
      this.currentTimeObj[this.ID] = this.currentTime;
      if(e.detail.currentTime === e.detail.duration){
        // 播放到最后设置回0 或者删掉
        this.currentTimeObj[this.ID] = 0;
      }
      uni.setStorageSync("currentTimeObj", JSON.stringify(this.currentTimeObj));
    },
    getVideo(){
      // 在视频的数据src获取到后
       this.$nextTick(() => {
        console.log('上次断开时间:',this.currentTime)
        // 获取video元素
        let videoDom = document.querySelector('#demoVideo video');
        if(this.isAndroid()){
          videoDom.currentTime = this.currentTime;
        }else{
          // 需要监听视频加载完成后才设置currentTime,否则对ios无效
          videoDom.addEventListener('canplay',() => {
            videoDom.currentTime = this.currentTime;
          });
        }
      })
    }
  },
  onLoad(option) {
    // 一进页面获取缓存所有视频播放记录
    if(uni.getStorageSync("currentTimeObj")){
      this.currentTimeObj = JSON.parse(uni.getStorageSync("currentTimeObj")) || {};
      this.currentTime = this.currentTimeObj[this.ID];
      console.log(this.currentTime)
    }
  },
</script>

文章作者: Mr. Zhan
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Mr. Zhan !
 上一篇
小程序内嵌h5 小程序内嵌h5
小程序内嵌h5a页面: <view bindtap='jumpToH5'>跳转</view> // js代码: jumpToH5: function () { wx.navigateTo({ url: '/
2020-08-23 Mr. Zhan
下一篇 
使用Vue.observable()进行状态管理 使用Vue.observable()进行状态管理
使用Vue.observable()进行状态管理用法:让一个对象可响应。Vue 内部会用它来处理 data 函数返回的对象。返回的对象可以直接用于渲染函数和计算属性内,并且会在发生变更时触发相应的更新。也可以作为最小化的跨组件状态存储器,用
2020-07-11 Mr. Zhan
  目录