当前位置:首页 > VUE

vue实现视频播放组件

2026-01-21 09:44:04VUE

实现基础视频播放组件

使用HTML5的<video>标签结合Vue的响应式数据绑定实现基础播放功能。

<template>
  <div class="video-player">
    <video 
      ref="videoPlayer"
      :src="videoSrc"
      @play="onPlay"
      @pause="onPause"
      @ended="onEnded"
      controls
    ></video>
    <div class="controls">
      <button @click="togglePlay">{{ isPlaying ? '暂停' : '播放' }}</button>
      <input type="range" v-model="progress" @input="seekVideo" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      videoSrc: 'https://example.com/sample.mp4',
      isPlaying: false,
      progress: 0
    };
  },
  methods: {
    togglePlay() {
      const video = this.$refs.videoPlayer;
      this.isPlaying ? video.pause() : video.play();
    },
    onPlay() {
      this.isPlaying = true;
    },
    onPause() {
      this.isPlaying = false;
    },
    onEnded() {
      this.isPlaying = false;
      this.progress = 0;
    },
    seekVideo(e) {
      const video = this.$refs.videoPlayer;
      const seekTime = (e.target.value / 100) * video.duration;
      video.currentTime = seekTime;
    }
  }
};
</script>

添加进度条实时更新

通过timeupdate事件监听播放进度,并更新进度条。

mounted() {
  this.$refs.videoPlayer.addEventListener('timeupdate', this.updateProgress);
},
methods: {
  updateProgress() {
    const video = this.$refs.videoPlayer;
    this.progress = (video.currentTime / video.duration) * 100;
  }
}

支持自定义控件样式

通过CSS覆盖默认控件样式,隐藏原生控件并完全自定义UI。

<video ref="videoPlayer" :src="videoSrc" @click="togglePlay" class="custom-video"></video>
<style>
.custom-video {
  width: 100%;
  /* 隐藏原生控件 */
  &::-webkit-media-controls {
    display: none !important;
  }
}
.controls {
  background: rgba(0, 0, 0, 0.5);
  padding: 10px;
}
</style>

集成第三方播放器库(如Video.js)

通过Vue封装第三方库实现高级功能(如字幕、多清晰度切换)。

  1. 安装依赖:
    npm install video.js @videojs-player/vue
  2. 组件封装:
    import { VideoJsPlayer } from '@videojs-player/vue';
    export default {
    components: { VideoJsPlayer },
    data() {
     return {
       options: {
         sources: [{
           src: 'https://example.com/video.mp4',
           type: 'video/mp4'
         }],
         controls: true,
         poster: 'https://example.com/poster.jpg'
       }
     };
    }
    };
    <template>
    <video-js-player :options="options" />
    </template>

响应式尺寸调整

通过CSS和Vue监听窗口大小变化,动态调整播放器尺寸。

data() {
  return {
    playerWidth: '800px'
  };
},
created() {
  window.addEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    this.playerWidth = window.innerWidth < 768 ? '100%' : '800px';
  }
}
<video :style="{ width: playerWidth }"></video>

跨浏览器兼容性处理

针对不同浏览器添加多种视频格式源。

vue实现视频播放组件

<video controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.webm" type="video/webm">
  <p>您的浏览器不支持HTML5视频</p>
</video>

性能优化

  • 使用preload="metadata"减少初始加载时间
  • 实现懒加载:通过Intersection ObserverAPI在视频进入视口时加载
    mounted() {
    const observer = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) {
        this.loadVideo();
        observer.unobserve(this.$el);
      }
    });
    observer.observe(this.$el);
    }

分享给朋友:

相关文章

vue 实现组件刷新

vue 实现组件刷新

实现组件刷新的方法 在Vue中,组件刷新通常指重新渲染组件或重置组件状态。以下是几种常见的实现方式: 使用v-if控制渲染 通过v-if指令可以销毁并重新创建组件: <template>…

vue动态组件实现

vue动态组件实现

动态组件的基本用法 在Vue中,动态组件通过<component>标签和is属性实现。is属性可以绑定组件名称或组件选项对象,实现动态切换。 <template> <…

如何判断react组件

如何判断react组件

判断 React 组件的依据 React 组件的判断通常基于其定义方式、功能特性和代码结构。以下是几种常见的判断方法: 函数组件与类组件 函数组件是通过 JavaScript 函数定义的,接收 pr…

vue实现递归组件

vue实现递归组件

递归组件的实现方法 在Vue中实现递归组件主要依靠组件调用自身的能力。以下是几种常见的实现方式: 使用组件name属性 通过组件的name属性实现递归调用是最简单的方式: <template…

vue 实现toast组件

vue 实现toast组件

实现 Vue Toast 组件的方法 创建基础 Toast 组件 在 Vue 项目中创建一个基础 Toast 组件,通常包含消息内容和显示控制逻辑。 <template> <d…

vue实现组件缓存

vue实现组件缓存

Vue 组件缓存的实现方法 在 Vue 中实现组件缓存通常使用 <keep-alive> 内置组件,它可以缓存不活动的组件实例,避免重复渲染和销毁。 基本用法 <keep-aliv…