当前位置:首页 > 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中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…

vue实现tabs

vue实现tabs

Vue实现Tabs组件的方法 使用动态组件和v-for指令 在Vue中创建Tabs组件可以通过动态组件和v-for指令实现。定义一个包含tab标题和内容的数组,使用v-for渲染tab标题,并通过点击…