当前位置:首页 > VUE

vue实现视频

2026-03-07 08:19:15VUE

Vue 实现视频播放功能

使用 HTML5 <video> 标签

Vue 可以直接使用 HTML5 的 <video> 标签实现视频播放。通过绑定 src 属性和控制播放状态,可以轻松实现视频功能。

vue实现视频

<template>
  <div>
    <video ref="videoPlayer" controls :src="videoSrc" @play="onPlay" @pause="onPause"></video>
    <button @click="togglePlay">{{ isPlaying ? 'Pause' : 'Play' }}</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videoSrc: 'path/to/video.mp4',
      isPlaying: false
    }
  },
  methods: {
    togglePlay() {
      if (this.isPlaying) {
        this.$refs.videoPlayer.pause()
      } else {
        this.$refs.videoPlayer.play()
      }
    },
    onPlay() {
      this.isPlaying = true
    },
    onPause() {
      this.isPlaying = false
    }
  }
}
</script>

使用第三方库(如 video.js

如果需要更丰富的功能(如自定义控件、字幕支持等),可以集成 video.js 或其他视频播放库。

vue实现视频

<template>
  <div>
    <video ref="videoPlayer" class="video-js"></video>
  </div>
</template>

<script>
import videojs from 'video.js'
import 'video.js/dist/video-js.css'

export default {
  data() {
    return {
      player: null
    }
  },
  mounted() {
    this.player = videojs(this.$refs.videoPlayer, {
      controls: true,
      sources: [{
        src: 'path/to/video.mp4',
        type: 'video/mp4'
      }]
    })
  },
  beforeDestroy() {
    if (this.player) {
      this.player.dispose()
    }
  }
}
</script>

实现视频流(如 HLS/DASH)

对于流媒体视频(如 .m3u8.mpd 格式),可以使用 hls.jsdash.js 库。

<template>
  <div>
    <video ref="videoPlayer" controls></video>
  </div>
</template>

<script>
import Hls from 'hls.js'

export default {
  mounted() {
    const videoSrc = 'path/to/stream.m3u8'
    const video = this.$refs.videoPlayer

    if (Hls.isSupported()) {
      const hls = new Hls()
      hls.loadSource(videoSrc)
      hls.attachMedia(video)
      hls.on(Hls.Events.MANIFEST_PARSED, () => {
        video.play()
      })
    } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
      video.src = videoSrc
      video.addEventListener('loadedmetadata', () => {
        video.play()
      })
    }
  }
}
</script>

自定义视频控件

可以通过 Vue 的响应式特性自定义视频控件,例如进度条、音量控制等。

<template>
  <div>
    <video ref="videoPlayer" :src="videoSrc" @timeupdate="updateProgress"></video>
    <div class="controls">
      <input type="range" v-model="progress" min="0" max="100" @input="seekTo">
      <button @click="toggleMute">{{ isMuted ? 'Unmute' : 'Mute' }}</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videoSrc: 'path/to/video.mp4',
      progress: 0,
      isMuted: false
    }
  },
  methods: {
    updateProgress() {
      const video = this.$refs.videoPlayer
      this.progress = (video.currentTime / video.duration) * 100
    },
    seekTo() {
      const video = this.$refs.videoPlayer
      video.currentTime = (this.progress / 100) * video.duration
    },
    toggleMute() {
      const video = this.$refs.videoPlayer
      video.muted = !video.muted
      this.isMuted = video.muted
    }
  }
}
</script>

以上方法可以根据需求选择或组合使用,实现不同复杂度的视频播放功能。

标签: 视频vue
分享给朋友:

相关文章

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="modal-o…

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式:…

vue 实现fadeout

vue 实现fadeout

Vue 实现淡出效果 在 Vue 中实现淡出(fade-out)效果可以通过 CSS 过渡或动画结合 Vue 的动态渲染功能完成。以下是几种常见方法: 使用 Vue 过渡(Transition)组件…

vue实现treeselect

vue实现treeselect

Vue TreeSelect 实现方法 使用 Vue 实现 TreeSelect 组件可以通过以下几种方式完成,包括使用现成的第三方库或自行封装组件。 使用第三方库 推荐使用成熟的第三方 TreeS…

vue键盘实现

vue键盘实现

监听键盘事件 在Vue中监听键盘事件可以通过v-on指令或@缩写实现。常见的键盘事件包括keydown、keyup和keypress。例如监听回车键事件: <template> &l…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…