当前位置:首页 > VUE

vue实现播放视频

2026-02-17 18:52:33VUE

使用 <video> 标签嵌入视频

在 Vue 中可以直接使用 HTML5 的 <video> 标签播放视频。通过 src 属性指定视频路径,controls 属性显示播放控件。

<template>
  <video controls width="600">
    <source src="/path/to/video.mp4" type="video/mp4">
    Your browser does not support the video tag.
  </video>
</template>

动态绑定视频源

通过 Vue 的响应式数据动态切换视频源,使用 v-bind 绑定 src 属性。

<template>
  <video controls :src="currentVideo"></video>
  <button @click="changeVideo">切换视频</button>
</template>

<script>
export default {
  data() {
    return {
      currentVideo: '/path/to/video1.mp4',
      videos: ['/path/to/video1.mp4', '/path/to/video2.mp4']
    }
  },
  methods: {
    changeVideo() {
      this.currentVideo = this.videos[1];
    }
  }
}
</script>

使用第三方库(如 Video.js)

对于更复杂的播放器功能(如自定义 UI、字幕支持),可以集成 Video.js。

安装依赖:

vue实现播放视频

npm install video.js

在 Vue 中初始化 Video.js:

<template>
  <video ref="videoPlayer" class="video-js"></video>
</template>

<script>
import videojs from 'video.js';
import 'video.js/dist/video-js.css';

export default {
  mounted() {
    this.player = videojs(this.$refs.videoPlayer, {
      controls: true,
      sources: [{
        src: '/path/to/video.mp4',
        type: 'video/mp4'
      }]
    });
  },
  beforeDestroy() {
    if (this.player) {
      this.player.dispose();
    }
  }
}
</script>

实现视频直播(HLS/DASH)

使用 hls.jsdash.js 库支持流媒体协议。以 HLS 为例:

vue实现播放视频

安装依赖:

npm install hls.js

集成 HLS 播放:

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

<script>
import Hls from 'hls.js';

export default {
  mounted() {
    const videoSrc = 'https://example.com/live/stream.m3u8';
    const videoElement = this.$refs.videoPlayer;

    if (Hls.isSupported()) {
      const hls = new Hls();
      hls.loadSource(videoSrc);
      hls.attachMedia(videoElement);
    } else if (videoElement.canPlayType('application/vnd.apple.mpegurl')) {
      videoElement.src = videoSrc;
    }
  }
}
</script>

自定义播放器控件

通过 Vue 的指令和事件实现自定义播放、暂停等功能:

<template>
  <div>
    <video ref="videoPlayer" :src="videoSrc" @timeupdate="updateProgress"></video>
    <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
    <input type="range" v-model="progress" max="100">
  </div>
</template>

<script>
export default {
  data() {
    return {
      videoSrc: '/path/to/video.mp4',
      isPlaying: false,
      progress: 0
    }
  },
  methods: {
    togglePlay() {
      const video = this.$refs.videoPlayer;
      this.isPlaying ? video.pause() : video.play();
      this.isPlaying = !this.isPlaying;
    },
    updateProgress() {
      const video = this.$refs.videoPlayer;
      this.progress = (video.currentTime / video.duration) * 100;
    }
  }
}
</script>

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

相关文章

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在V…

vue 实现pc

vue 实现pc

Vue 实现 PC 端应用的核心要点 Vue.js 是一个渐进式框架,适合构建 PC 端单页应用(SPA)或复杂的前端界面。以下是实现 PC 端应用的关键方法和注意事项: 项目初始化与工程化配置…

vue实现分类

vue实现分类

Vue实现分类功能的方法 使用v-for和计算属性 通过计算属性对数据进行分类处理,结合v-for渲染分类后的列表。示例代码: <template> <div>…

vue实现遮罩

vue实现遮罩

Vue 实现遮罩层的方法 在 Vue 中实现遮罩层可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-show 或 v-if 控制显示 通过 Vue 的指令动态控制遮罩层的显示与隐…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…