当前位置:首页 > 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 的响应式特性完全自定义播放器控件:

前端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 组件展示新闻列表,通常使用 v-for…

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</p&…

vue for实现

vue for实现

Vue 中的 v-for 指令 v-for 是 Vue.js 中用于渲染列表数据的核心指令,通过遍历数组或对象生成动态内容。 基本语法(数组) <ul> <li v-for="…