当前位置:首页 > VUE

vue实现录播播放

2026-01-07 01:39:59VUE

Vue 实现录播播放

使用 video.js 实现

安装 video.js 和相关的 Vue 适配器:

npm install video.js @videojs-player/vue

在 Vue 组件中引入并使用:

<template>
  <video-player
    src="你的视频地址.mp4"
    controls
    autoplay
    :loop="false"
    :volume="0.6"
  />
</template>

<script>
import { defineComponent } from 'vue'
import { VideoPlayer } from '@videojs-player/vue'
import 'video.js/dist/video-js.css'

export default defineComponent({
  components: {
    VideoPlayer
  }
})
</script>

使用原生 HTML5 video 元素

简单的视频播放器实现:

<template>
  <div>
    <video
      ref="videoPlayer"
      controls
      width="640"
      height="360"
    >
      <source src="你的视频地址.mp4" type="video/mp4">
      您的浏览器不支持 HTML5 视频
    </video>
    <div>
      <button @click="play">播放</button>
      <button @click="pause">暂停</button>
      <button @click="stop">停止</button>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    play() {
      this.$refs.videoPlayer.play()
    },
    pause() {
      this.$refs.videoPlayer.pause()
    },
    stop() {
      const video = this.$refs.videoPlayer
      video.pause()
      video.currentTime = 0
    }
  }
}
</script>

添加播放列表功能

实现多个视频的播放列表:

<template>
  <div>
    <video
      ref="videoPlayer"
      controls
      width="640"
      height="360"
    ></video>
    <ul>
      <li
        v-for="(video, index) in videos"
        :key="index"
        @click="changeVideo(video.src)"
      >
        {{ video.title }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videos: [
        { title: '视频1', src: 'video1.mp4' },
        { title: '视频2', src: 'video2.mp4' },
        { title: '视频3', src: 'video3.mp4' }
      ]
    }
  },
  methods: {
    changeVideo(src) {
      this.$refs.videoPlayer.src = src
      this.$refs.videoPlayer.load()
      this.$refs.videoPlayer.play()
    }
  }
}
</script>

添加进度条控制

自定义视频进度控制:

<template>
  <div>
    <video
      ref="videoPlayer"
      @timeupdate="updateProgress"
      controls
      width="640"
      height="360"
    ></video>
    <input
      type="range"
      min="0"
      max="100"
      step="1"
      v-model="progress"
      @input="seek"
    />
    <span>{{ currentTime }} / {{ duration }}</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      progress: 0,
      currentTime: '00:00',
      duration: '00:00'
    }
  },
  methods: {
    updateProgress() {
      const video = this.$refs.videoPlayer
      this.progress = (video.currentTime / video.duration) * 100
      this.currentTime = this.formatTime(video.currentTime)
      this.duration = this.formatTime(video.duration)
    },
    seek() {
      const video = this.$refs.videoPlayer
      video.currentTime = (this.progress / 100) * video.duration
    },
    formatTime(seconds) {
      const minutes = Math.floor(seconds / 60)
      const secs = Math.floor(seconds % 60)
      return `${minutes}:${secs < 10 ? '0' : ''}${secs}`
    }
  }
}
</script>

添加全屏功能

实现全屏播放功能:

<template>
  <div>
    <video
      ref="videoPlayer"
      controls
      width="640"
      height="360"
    ></video>
    <button @click="toggleFullscreen">全屏</button>
  </div>
</template>

<script>
export default {
  methods: {
    toggleFullscreen() {
      const video = this.$refs.videoPlayer
      if (!document.fullscreenElement) {
        video.requestFullscreen()
      } else {
        document.exitFullscreen()
      }
    }
  }
}
</script>

添加画中画功能

实现画中画模式:

<template>
  <div>
    <video
      ref="videoPlayer"
      controls
      width="640"
      height="360"
    ></video>
    <button @click="togglePictureInPicture">画中画</button>
  </div>
</template>

<script>
export default {
  methods: {
    async togglePictureInPicture() {
      const video = this.$refs.videoPlayer
      try {
        if (video !== document.pictureInPictureElement) {
          await video.requestPictureInPicture()
        } else {
          await document.exitPictureInPicture()
        }
      } catch (error) {
        console.error('画中画错误:', error)
      }
    }
  }
}
</script>

添加播放速度控制

实现播放速度调整:

<template>
  <div>
    <video
      ref="videoPlayer"
      controls
      width="640"
      height="360"
    ></video>
    <select v-model="playbackRate" @change="changeSpeed">
      <option value="0.5">0.5x</option>
      <option value="1" selected>1x</option>
      <option value="1.5">1.5x</option>
      <option value="2">2x</option>
    </select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      playbackRate: 1
    }
  },
  methods: {
    changeSpeed() {
      this.$refs.videoPlayer.playbackRate = this.playbackRate
    }
  }
}
</script>

这些实现方式可以根据实际需求进行组合和扩展,构建功能更完善的录播播放器。

vue实现录播播放

标签: vue
分享给朋友:

相关文章

vue实现购物按钮

vue实现购物按钮

Vue 购物按钮实现方法 基础按钮实现 使用 Vue 的模板语法创建基础按钮组件,绑定点击事件处理购物逻辑: <template> <button @click="addTo…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click…

vue实现语音

vue实现语音

Vue 实现语音功能的方法 使用 Web Speech API Vue 可以结合 Web Speech API 实现语音识别和合成功能。Web Speech API 包含 SpeechRecognit…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生…