当前位置:首页 > VUE

vue实现视频观看时长

2026-02-23 11:35:18VUE

监听视频播放事件

使用Vue的@play事件监听视频开始播放,结合@timeupdate实时更新当前播放时间。在<video>标签上绑定事件:

<template>
  <video 
    ref="videoPlayer"
    @play="onPlay"
    @pause="onPause"
    @timeupdate="onTimeUpdate"
    src="video.mp4"
  ></video>
</template>

记录播放时间

通过currentTime属性获取当前播放进度,并计算累计时长。在Vue组件的data中定义变量存储时间:

vue实现视频观看时长

data() {
  return {
    startTime: 0,
    currentTime: 0,
    totalWatchedTime: 0,
    isPlaying: false
  };
},
methods: {
  onPlay() {
    this.startTime = Date.now();
    this.isPlaying = true;
  },
  onPause() {
    if (this.isPlaying) {
      const endTime = Date.now();
      this.totalWatchedTime += (endTime - this.startTime) / 1000;
      this.isPlaying = false;
    }
  },
  onTimeUpdate() {
    this.currentTime = this.$refs.videoPlayer.currentTime;
  }
}

防抖处理频繁更新

为避免@timeupdate事件触发过于频繁,使用Lodash的debounce函数优化性能:

vue实现视频观看时长

import { debounce } from 'lodash';

methods: {
  onTimeUpdate: debounce(function() {
    this.currentTime = this.$refs.videoPlayer.currentTime;
  }, 500)
}

持久化存储时长

通过localStorage或API请求保存累计观看时长,确保页面刷新后数据不丢失:

onPause() {
  if (this.isPlaying) {
    const endTime = Date.now();
    this.totalWatchedTime += (endTime - this.startTime) / 1000;
    localStorage.setItem('videoWatchedTime', this.totalWatchedTime);
    this.isPlaying = false;
  }
},
mounted() {
  const savedTime = localStorage.getItem('videoWatchedTime');
  if (savedTime) {
    this.totalWatchedTime = parseFloat(savedTime);
  }
}

完整组件示例

整合以上逻辑的完整Vue组件代码:

<template>
  <div>
    <video 
      ref="videoPlayer"
      @play="onPlay"
      @pause="onPause"
      @timeupdate="onTimeUpdate"
      src="video.mp4"
    ></video>
    <p>已观看:{{ totalWatchedTime.toFixed(2) }}秒</p>
  </div>
</template>

<script>
import { debounce } from 'lodash';

export default {
  data() {
    return {
      startTime: 0,
      totalWatchedTime: 0,
      isPlaying: false
    };
  },
  methods: {
    onPlay() {
      this.startTime = Date.now();
      this.isPlaying = true;
    },
    onPause() {
      if (this.isPlaying) {
        const endTime = Date.now();
        this.totalWatchedTime += (endTime - this.startTime) / 1000;
        localStorage.setItem('videoWatchedTime', this.totalWatchedTime);
        this.isPlaying = false;
      }
    },
    onTimeUpdate: debounce(function() {
      this.currentTime = this.$refs.videoPlayer.currentTime;
    }, 500)
  },
  mounted() {
    const savedTime = localStorage.getItem('videoWatchedTime');
    if (savedTime) {
      this.totalWatchedTime = parseFloat(savedTime);
    }
  }
};
</script>

标签: 时长视频
分享给朋友:

相关文章

vue实现视频

vue实现视频

Vue 实现视频播放功能 Vue 可以通过集成第三方库或直接使用 HTML5 的 <video> 标签来实现视频播放功能。以下是几种常见的方法: 使用 HTML5 <video&…

vue实现缓存视频

vue实现缓存视频

Vue 实现视频缓存的方法 在 Vue 中实现视频缓存可以通过多种方式完成,包括使用浏览器的缓存机制、Service Worker 或者借助第三方库。以下是几种常见的方法: 使用 Service W…

vue项目实现视频

vue项目实现视频

视频播放基础实现 在Vue项目中实现视频播放,可以使用HTML5的<video>标签。以下是一个基础示例: <template> <div> &…

vue 视频android实现

vue 视频android实现

在Vue中实现Android端的视频功能,可以通过以下方法完成,结合WebView或原生混合开发方案: 使用WebView嵌入H5视频 适用于轻量级需求,通过Vue构建H5页面,在Android W…

vue实现视频预览

vue实现视频预览

Vue 实现视频预览的方法 在 Vue 中实现视频预览功能通常需要结合 HTML5 的 <video> 标签和 Vue 的事件处理机制。以下是几种常见的实现方式: 使用 HTML5 &…

vue如何实现视频

vue如何实现视频

Vue 实现视频功能的方法 在 Vue 中实现视频功能可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 <video> 标签 Vue 可以直接在模板中使用 HTML5 的…