当前位置:首页 > 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实时视频通话的技术方案 WebRTC是实现H5实时视频通话的核心技术,无需插件即可在浏览器中实现点对点通信。以下是关键实现步骤: 基础环境准备 确保使用支持WebRTC的现代浏览器(Chro…

php实现视频直播

php实现视频直播

PHP 实现视频直播的基本方法 PHP 本身并不直接处理视频流,但可以作为后端支持视频直播系统的搭建。以下是几种常见的实现方式: 使用第三方流媒体服务 利用现成的流媒体平台(如 Wowza、Red5…

vue项目实现视频

vue项目实现视频

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

vue实现视频背景

vue实现视频背景

使用 Vue 实现视频背景 在 Vue 中实现视频背景可以通过多种方式完成,以下是一些常见的方法: 使用 HTML5 <video> 标签 在 Vue 组件的模板中直接使用 <vi…

vue实现视频列表

vue实现视频列表

Vue 实现视频列表 在 Vue 中实现视频列表通常需要结合数据绑定、组件化和第三方库(如视频播放器)。以下是具体实现方法: 数据准备 创建一个数组存储视频信息,包括标题、封面图、视频链接等: d…

vue视频直播实现

vue视频直播实现

实现 Vue 视频直播的基本方法 使用 WebRTC 实现点对点直播 WebRTC 是浏览器原生支持的实时通信技术,适合低延迟场景。在 Vue 项目中安装 peerjs 或 simple-peer 库…