当前位置:首页 > 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。

安装依赖:

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 为例:

安装依赖:

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 的指令和事件实现自定义播放、暂停等功能:

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中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框用…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

vue jwt实现

vue jwt实现

Vue JWT 实现方法 安装依赖 确保项目中安装了 jsonwebtoken(后端)和 axios(前端)。若使用 Vue 3,可搭配 vue-router 和 pinia(或 vuex)管理状态。…

vue实现翻页

vue实现翻页

Vue实现翻页功能 在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑。以下是常见的实现方法: 使用Element UI分页组件 安装Element UI后,可以直接使用其分页组件:…