当前位置:首页 > VUE

vue实现播放视频

2026-03-29 19:04:54VUE

使用 <video> 标签实现基础播放

在 Vue 模板中直接使用 HTML5 的 <video> 标签,通过 ref 绑定控制播放状态:

<template>
  <div>
    <video ref="videoPlayer" controls width="600">
      <source src="/path/to/video.mp4" type="video/mp4">
    </video>
    <button @click="play">播放</button>
    <button @click="pause">暂停</button>
  </div>
</template>

<script>
export default {
  methods: {
    play() {
      this.$refs.videoPlayer.play()
    },
    pause() {
      this.$refs.videoPlayer.pause()
    }
  }
}
</script>

通过第三方库实现高级功能

安装 video.js 库实现自定义控件和跨浏览器支持:

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

调用示例:

<VideoPlayer :options="{
  autoplay: false,
  controls: true,
  sources: [{
    src: '/path/to/video.mp4',
    type: 'video/mp4'
  }]
}"/>

处理直播流媒体

使用 hls.js 播放 m3u8 格式的直播流:

vue实现播放视频

npm install hls.js

实现代码:

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

<script>
import Hls from 'hls.js'

export default {
  data() {
    return {
      hls: null
    }
  },
  mounted() {
    if (Hls.isSupported()) {
      const video = this.$refs.videoPlayer
      this.hls = new Hls()
      this.hls.loadSource('http://example.com/live.m3u8')
      this.hls.attachMedia(video)
    }
  },
  beforeDestroy() {
    if (this.hls) {
      this.hls.destroy()
    }
  }
}
</script>

实现自定义播放器控件

通过 Vue 数据绑定实现进度条控制:

<template>
  <div>
    <video 
      ref="videoPlayer"
      @timeupdate="updateProgress"
      @loadedmetadata="setDuration">
    </video>
    <input 
      type="range" 
      v-model="progress"
      @input="seekTo">
  </div>
</template>

<script>
export default {
  data() {
    return {
      progress: 0,
      duration: 0
    }
  },
  methods: {
    updateProgress() {
      const video = this.$refs.videoPlayer
      this.progress = (video.currentTime / this.duration) * 100
    },
    setDuration() {
      this.duration = this.$refs.videoPlayer.duration
    },
    seekTo() {
      const video = this.$refs.videoPlayer
      video.currentTime = (this.progress / 100) * this.duration
    }
  }
}
</script>

响应式视频尺寸处理

通过 CSS 和 Vue 结合实现响应式布局:

<template>
  <div class="video-container">
    <video ref="videoPlayer" :style="videoStyle"></video>
  </div>
</template>

<script>
export default {
  data() {
    return {
      windowWidth: window.innerWidth
    }
  },
  computed: {
    videoStyle() {
      return {
        width: this.windowWidth > 768 ? '800px' : '100%',
        height: this.windowWidth > 768 ? '450px' : 'auto'
      }
    }
  },
  mounted() {
    window.addEventListener('resize', this.handleResize)
  },
  methods: {
    handleResize() {
      this.windowWidth = window.innerWidth
    }
  }
}
</script>

<style>
.video-container {
  position: relative;
  padding-bottom: 56.25%; /* 16:9 Aspect Ratio */
}

.video-container video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>

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

相关文章

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="bu…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signatu…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…