当前位置:首页 > VUE

vue实现视频观看时长

2026-01-22 20:47:41VUE

Vue 实现视频观看时长统计

在 Vue 中实现视频观看时长统计,可以通过监听视频播放事件并结合时间计算来完成。以下是具体实现方法:

监听视频事件

在 Vue 组件中,使用 ref 获取视频元素,并监听 timeupdateplaypause 等事件:

vue实现视频观看时长

<template>
  <video ref="videoPlayer" @timeupdate="handleTimeUpdate" @play="handlePlay" @pause="handlePause">
    <source src="video.mp4" type="video/mp4">
  </video>
  <p>已观看时长:{{ watchedTime }} 秒</p>
</template>

<script>
export default {
  data() {
    return {
      watchedTime: 0,
      lastPlayTime: 0,
      isPlaying: false
    }
  },
  methods: {
    handleTimeUpdate() {
      if (this.isPlaying) {
        const currentTime = this.$refs.videoPlayer.currentTime
        this.watchedTime += currentTime - this.lastPlayTime
        this.lastPlayTime = currentTime
      }
    },
    handlePlay() {
      this.isPlaying = true
      this.lastPlayTime = this.$refs.videoPlayer.currentTime
    },
    handlePause() {
      this.isPlaying = false
    }
  }
}
</script>

使用计时器统计时长

另一种方法是使用 setInterval 定时器统计播放时长:

vue实现视频观看时长

methods: {
  startTimer() {
    this.timer = setInterval(() => {
      if (this.isPlaying) {
        this.watchedTime += 1
      }
    }, 1000)
  },
  handlePlay() {
    this.isPlaying = true
    this.startTimer()
  },
  handlePause() {
    this.isPlaying = false
    clearInterval(this.timer)
  }
},
mounted() {
  this.startTimer()
},
beforeDestroy() {
  clearInterval(this.timer)
}

保存观看进度

如果需要保存用户的观看进度,可以使用 localStorage:

methods: {
  saveProgress() {
    localStorage.setItem('videoProgress', this.watchedTime)
  },
  loadProgress() {
    const savedTime = localStorage.getItem('videoProgress')
    if (savedTime) {
      this.watchedTime = parseInt(savedTime)
      this.$refs.videoPlayer.currentTime = this.watchedTime
    }
  }
},
mounted() {
  this.loadProgress()
},
beforeDestroy() {
  this.saveProgress()
}

格式化显示时间

将秒数格式化为更友好的时间显示:

computed: {
  formattedTime() {
    const hours = Math.floor(this.watchedTime / 3600)
    const minutes = Math.floor((this.watchedTime % 3600) / 60)
    const seconds = Math.floor(this.watchedTime % 60)

    return [
      hours.toString().padStart(2, '0'),
      minutes.toString().padStart(2, '0'),
      seconds.toString().padStart(2, '0')
    ].join(':')
  }
}

完整组件示例

<template>
  <div>
    <video 
      ref="videoPlayer"
      @timeupdate="handleTimeUpdate"
      @play="handlePlay"
      @pause="handlePause"
      controls
    >
      <source src="video.mp4" type="video/mp4">
    </video>
    <p>观看时长: {{ formattedTime }}</p>
    <p>当前进度: {{ progress }}%</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      watchedTime: 0,
      lastPlayTime: 0,
      isPlaying: false,
      timer: null
    }
  },
  computed: {
    formattedTime() {
      const hours = Math.floor(this.watchedTime / 3600)
      const minutes = Math.floor((this.watchedTime % 3600) / 60)
      const seconds = Math.floor(this.watchedTime % 60)
      return `${hours}:${minutes}:${seconds}`
    },
    progress() {
      const duration = this.$refs.videoPlayer?.duration || 1
      return ((this.watchedTime / duration) * 100).toFixed(2)
    }
  },
  methods: {
    handleTimeUpdate() {
      if (this.isPlaying) {
        const currentTime = this.$refs.videoPlayer.currentTime
        this.watchedTime += currentTime - this.lastPlayTime
        this.lastPlayTime = currentTime
      }
    },
    handlePlay() {
      this.isPlaying = true
      this.lastPlayTime = this.$refs.videoPlayer.currentTime
    },
    handlePause() {
      this.isPlaying = false
    },
    saveProgress() {
      localStorage.setItem('videoProgress', this.watchedTime)
    },
    loadProgress() {
      const savedTime = localStorage.getItem('videoProgress')
      if (savedTime) {
        this.watchedTime = parseInt(savedTime)
        this.$refs.videoPlayer.currentTime = this.watchedTime
      }
    }
  },
  mounted() {
    this.loadProgress()
  },
  beforeDestroy() {
    this.saveProgress()
  }
}
</script>

这些方法可以根据实际需求进行组合或调整,实现精确的视频观看时长统计功能。

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

相关文章

h5 实现视频通话

h5 实现视频通话

H5 实现视频通话的技术方案 H5(HTML5)实现视频通话主要依赖WebRTC(Web Real-Time Communication)技术。以下是实现步骤和关键代码示例: 获取用户媒体设备权限…

vue实现上传视频

vue实现上传视频

使用 <input type="file"> 实现基础上传 通过 HTML 原生文件选择控件获取视频文件,结合 Vue 处理上传逻辑。 <template> <…

vue实现视频录制

vue实现视频录制

实现视频录制的基本思路 在Vue中实现视频录制通常需要借助浏览器的MediaDevices API和MediaRecorder API。核心流程包括获取用户摄像头/麦克风权限、录制媒体流、保存录制结果…

vue实现视频预览

vue实现视频预览

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

vue实现视频功能

vue实现视频功能

Vue 实现视频播放功能 使用 Vue 实现视频功能可以通过原生 HTML5 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见方法: 使用原生 HTML5 &…

vue实现录视频

vue实现录视频

Vue 实现录视频功能 使用浏览器 API 实现录屏 在 Vue 项目中可以通过浏览器提供的 MediaDevices API 和 MediaRecorder API 实现录屏功能。需要确保浏览器支持…