【Vue】vue3 video 保存视频进度,每次进入加载上次的视频进度

  1. 使用 localStorage 存储每个视频的播放进度
  2. 在组件加载时恢复上次的播放进度
  3. 在视频播放过程中实时保存进度
  4. 在组件卸载前保存最终进度
  5. 使用 timeupdate 事件来监听视频播放进度的变化

在模板中为视频元素添加事件监听:

html 复制代码
<video
  loop
  autoplay
  controls
  :id="`video_${index}`"
  :src="getVideoSrc(video.src)"
  class="video"
  @loadedmetadata="loadVideoProgress"></video>
ts 复制代码
// ... 其他导入保持不变
import { reactive, ref, onMounted, onBeforeUnmount } from 'vue'

// ... paramsList 和 params 保持不变

// 添加视频进度保存的常量
const VIDEO_PROGRESS_KEY = 'VIDEO_PROGRESS'

const videoList = ref([
  {
    title: '大范围流场图',
    src: 'dfwlct',
    isPlaying: true,
    progress: 0, // 添加进度属性
  },
  {
    title: '工程局部流场图',
    src: 'gcjblct',
    isPlaying: true,
    progress: 0, // 添加进度属性
  },
])

// 加载保存的视频进度
const loadVideoProgress = () => {
  const savedProgress = localStorage.getItem(VIDEO_PROGRESS_KEY)
  if (savedProgress) {
    const progressData = JSON.parse(savedProgress)
    videoList.value.forEach((video, index) => {
      if (progressData[video.src]) {
        video.progress = progressData[video.src]
        const videoElement = document.getElementById(`video_${index}`) as HTMLVideoElement
        if (videoElement) {
          videoElement.currentTime = video.progress
        }
      }
    })
  }
}

// 保存视频进度
const saveVideoProgress = () => {
  const progressData = {}
  videoList.value.forEach((video, index) => {
    const videoElement = document.getElementById(`video_${index}`) as HTMLVideoElement
    if (videoElement) {
      progressData[video.src] = videoElement.currentTime
    }
  })
  localStorage.setItem(VIDEO_PROGRESS_KEY, JSON.stringify(progressData))
}

// 监听视频时间更新
const handleTimeUpdate = (index: number) => {
  const videoElement = document.getElementById(`video_${index}`) as HTMLVideoElement
  if (videoElement) {
    videoList.value[index].progress = videoElement.currentTime
    saveVideoProgress()
  }
}

// 组件挂载时加载进度
onMounted(() => {
  loadVideoProgress()
  // 为每个视频添加时间更新事件监听
  videoList.value.forEach((_, index) => {
    const videoElement = document.getElementById(`video_${index}`)
    if (videoElement) {
      videoElement.addEventListener('timeupdate', () => handleTimeUpdate(index))
    }
  })
})

// 组件卸载前移除事件监听
onBeforeUnmount(() => {
  videoList.value.forEach((_, index) => {
    const videoElement = document.getElementById(`video_${index}`)
    if (videoElement) {
      videoElement.removeEventListener('timeupdate', () => handleTimeUpdate(index))
    }
  })
  saveVideoProgress()
})

// ... 其他代码保持不变

这样,用户每次进入页面时都会自动加载上次观看的进度。进度信息会在以下情况下保存:

  • 视频播放过程中
  • 用户暂停视频时
  • 用户离开页面时
相关推荐
小小小小宇4 小时前
虚拟列表兼容老DOM操作
前端
悦悦子a啊4 小时前
Python之--基本知识
开发语言·前端·python
却道天凉_好个秋5 小时前
音视频学习(三十六):websocket协议总结
websocket·音视频
安全系统学习5 小时前
系统安全之大模型案例分析
前端·安全·web安全·网络安全·xss
涛哥码咖5 小时前
chrome安装AXURE插件后无效
前端·chrome·axure
OEC小胖胖5 小时前
告别 undefined is not a function:TypeScript 前端开发优势与实践指南
前端·javascript·typescript·web
行云&流水5 小时前
Vue3 Lifecycle Hooks
前端·javascript·vue.js
Sally璐璐6 小时前
零基础学HTML和CSS:网页设计入门
前端·css
老虎06276 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
三水气象台6 小时前
用户中心Vue3网页开发(1.0版)
javascript·css·vue.js·typescript·前端框架·html·anti-design-vue