当前位置:首页 > 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实现多人视频通话 使用Vue实现多人视频通话需要结合WebRTC技术和相关库。以下是关键步骤和代码示例: 安装依赖 需要安装peerjs和vue-webrtc等库: npm instal…

h5实现视频通话

h5实现视频通话

WebRTC 基础实现 H5 视频通话主要通过 WebRTC(Web Real-Time Communication)技术实现。现代浏览器原生支持该 API,无需插件即可完成点对点音视频传输。 安…

h5怎么实现视频直播

h5怎么实现视频直播

实现H5视频直播的方法 H5视频直播可以通过多种技术实现,主要包括使用HTML5的<video>标签结合流媒体协议,或借助第三方库和平台。以下是几种常见方法: 使用HLS(HTTP L…

h5 实现视频通话

h5 实现视频通话

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

vue实现视频通话

vue实现视频通话

Vue实现视频通话的方法 使用WebRTC技术 WebRTC是一种支持浏览器之间实时通信的技术,无需插件即可实现视频通话。Vue可以结合WebRTC的API实现视频通话功能。 安装必要的依赖库:…

vue实现上传视频

vue实现上传视频

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