当前位置:首页 > 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>

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

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

相关文章

vue视频怎么实现

vue视频怎么实现

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

css帧动画制作视频

css帧动画制作视频

使用CSS关键帧(@keyframes)制作动画 定义关键帧动画,通过@keyframes规则指定动画在不同时间点的样式变化。例如创建一个元素从左到右移动的动画: @keyframes slide…

css飞机制作视频

css飞机制作视频

使用CSS制作飞机动画 通过CSS的动画和变形属性,可以创建一个简单的飞机动画效果。以下是实现步骤: HTML结构 <div class="airplane"></div…

uniapp视频

uniapp视频

uniapp视频开发基础 uniapp支持通过video组件实现视频播放功能,该组件兼容多端(H5、小程序、App等),基本用法如下: <template> <video…

h5 实现视频通话

h5 实现视频通话

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

vue视频怎么实现

vue视频怎么实现

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