当前位置:首页 > 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中实现单词反转功能,可以通过多种方式完成。核心思路是利用JavaScript的字符串处理方法,将输入的单词或句子拆分为数组,反转数组后再拼接成字符串。 方法一:使用计算属性 通过计…

vue实现公告

vue实现公告

Vue 实现公告功能的方法 公告功能通常需要实现滚动展示、自动切换或固定显示的效果。以下是几种常见的实现方式: 使用 marquee 标签实现滚动公告 <template> <…

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

vue实现flbook

vue实现flbook

Vue 实现类似 Flbook 的翻页效果 要实现类似 Flbook 的翻页效果,可以使用 Vue 结合 CSS 动画和 JavaScript 事件处理。以下是实现方法: 安装依赖 需要安装 vue…

vue实现上划

vue实现上划

Vue 实现上划功能 在Vue中实现上划功能,可以通过监听触摸事件(touchstart、touchmove、touchend)来判断用户的手势方向。以下是实现上划功能的几种方法: 监听触摸事件 通…

vue实现dialog

vue实现dialog

Vue 实现 Dialog 的方法 使用 Vue 原生组件实现 Vue 可以通过组件化的方式实现 Dialog,以下是一个简单的实现示例: <template> <div>…