当前位置:首页 > 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中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue实现tooltips

vue实现tooltips

Vue 实现 Tooltips 的方法 使用原生 HTML title 属性 在 Vue 模板中直接使用 HTML 原生 title 属性实现简单的提示效果。这种方式无需额外依赖,但功能较为基础,无法…

vue实现type切换

vue实现type切换

Vue 实现 Type 切换的实现方法 在 Vue 中实现 Type 切换功能可以通过动态组件、条件渲染或路由切换等方式实现。以下是几种常见方法: 使用 v-if 或 v-show 条件渲染 通过绑…