当前位置:首页 > VUE

前端vue实现视频播放

2026-02-24 02:56:46VUE

Vue 实现视频播放的方法

使用 HTML5 <video> 标签

Vue 可以直接利用 HTML5 的 <video> 标签实现基础视频播放功能。通过绑定 src 和事件控制播放状态。

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

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

集成第三方库(如 Video.js)

对于更复杂的需求(如自定义 UI、多格式支持),可以使用 Video.js 等库。

安装依赖:

npm install video.js

示例代码:

<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 {
  props: ['options'],
  mounted() {
    this.player = videojs(
      this.$refs.videoPlayer,
      this.options,
      function onPlayerReady() {
        console.log('Player ready')
      }
    )
  },
  beforeDestroy() {
    if (this.player) {
      this.player.dispose()
    }
  }
}
</script>

使用 Vue 视频播放组件

社区提供的现成组件(如 vue-video-player)可以快速集成。

安装:

npm install vue-video-player

示例:

<template>
  <vue-video-player 
    :options="playerOptions"
    @ready="playerReadied"
  />
</template>

<script>
import { videoPlayer } from 'vue-video-player'

export default {
  components: {
    videoPlayer
  },
  data() {
    return {
      playerOptions: {
        sources: [{
          type: 'video/mp4',
          src: 'path/to/video.mp4'
        }],
        controls: true
      }
    }
  }
}
</script>

直播流播放(HLS/DASH)

对于直播或自适应码率视频,需使用 hls.js 或 dash.js 库。

HLS 示例:

<template>
  <video ref="videoEl" controls></video>
</template>

<script>
import Hls from 'hls.js'

export default {
  mounted() {
    const src = 'http://example.com/stream.m3u8'
    const video = this.$refs.videoEl

    if (Hls.isSupported()) {
      const hls = new Hls()
      hls.loadSource(src)
      hls.attachMedia(video)
    } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
      video.src = src
    }
  }
}
</script>

自定义控件开发

通过 Vue 的响应式特性完全自定义播放器控件:

<template>
  <div class="custom-player">
    <video 
      ref="video"
      :src="source"
      @timeupdate="updateTime"
    ></video>
    <div class="controls">
      <progress :value="currentTime" :max="duration"></progress>
      <button @click="togglePlay">{{ isPlaying ? '❚❚' : '►' }}</button>
      <span>{{ formattedTime }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      source: 'video.mp4',
      isPlaying: false,
      currentTime: 0,
      duration: 0
    }
  },
  computed: {
    formattedTime() {
      return new Date(this.currentTime * 1000)
        .toISOString()
        .substr(11, 8)
    }
  },
  methods: {
    togglePlay() {
      const video = this.$refs.video
      this.isPlaying ? video.pause() : video.play()
    },
    updateTime(e) {
      this.currentTime = e.target.currentTime
      this.duration = e.target.duration
    }
  }
}
</script>

前端vue实现视频播放

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

相关文章

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export defaul…

vue环境实现

vue环境实现

Vue 环境搭建 安装 Node.js 和 npm Node.js 是运行 Vue 的基础环境,npm 是包管理工具。从 Node.js 官网 下载并安装最新稳定版本。安装完成后,在终端运行以下命令验…

vue实现 hover

vue实现 hover

实现 Vue 中的 hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是常见的几种方法: 使用 CSS 伪类 最简单的方式是直接使用 CSS 的 :hover 伪类,无…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @…