当前位置:首页 > VUE

vue实现视频播放暂停

2026-02-21 02:49:35VUE

使用 <video> 标签实现基础控制

在 Vue 中可以通过原生 HTML5 的 <video> 标签实现视频播放和暂停功能。在模板中添加 <video> 标签并绑定 ref,通过调用 play()pause() 方法控制视频。

vue实现视频播放暂停

<template>
  <div>
    <video ref="videoPlayer" controls>
      <source src="your-video.mp4" type="video/mp4">
    </video>
    <button @click="playVideo">播放</button>
    <button @click="pauseVideo">暂停</button>
  </div>
</template>

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

使用状态管理播放/暂停切换

通过一个布尔状态变量管理播放和暂停的切换,减少重复代码。定义一个 isPlaying 变量,根据当前状态调用对应方法。

vue实现视频播放暂停

<template>
  <div>
    <video ref="videoPlayer" controls>
      <source src="your-video.mp4" type="video/mp4">
    </video>
    <button @click="togglePlay">
      {{ isPlaying ? '暂停' : '播放' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isPlaying: false
    };
  },
  methods: {
    togglePlay() {
      const video = this.$refs.videoPlayer;
      if (this.isPlaying) {
        video.pause();
      } else {
        video.play();
      }
      this.isPlaying = !this.isPlaying;
    }
  }
};
</script>

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

对于更复杂的视频控制需求,可以集成第三方库如 Video.js。安装 Video.js 后,在 Vue 组件中初始化播放器并调用其 API。

npm install video.js
<template>
  <div>
    <video ref="videoPlayer" class="video-js"></video>
    <button @click="togglePlay">
      {{ isPlaying ? '暂停' : '播放' }}
    </button>
  </div>
</template>

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

export default {
  data() {
    return {
      player: null,
      isPlaying: false
    };
  },
  mounted() {
    this.player = videojs(this.$refs.videoPlayer, {
      sources: [{
        src: 'your-video.mp4',
        type: 'video/mp4'
      }]
    });
  },
  methods: {
    togglePlay() {
      if (this.isPlaying) {
        this.player.pause();
      } else {
        this.player.play();
      }
      this.isPlaying = !this.isPlaying;
    }
  },
  beforeDestroy() {
    if (this.player) {
      this.player.dispose();
    }
  }
};
</script>

监听视频事件更新状态

通过监听 <video> 标签的 playpause 事件,自动同步 isPlaying 状态。避免因用户直接操作视频控件导致状态不一致。

<template>
  <div>
    <video 
      ref="videoPlayer" 
      controls
      @play="onPlay"
      @pause="onPause"
    >
      <source src="your-video.mp4" type="video/mp4">
    </video>
    <button @click="togglePlay">
      {{ isPlaying ? '暂停' : '播放' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isPlaying: false
    };
  },
  methods: {
    togglePlay() {
      const video = this.$refs.videoPlayer;
      if (this.isPlaying) {
        video.pause();
      } else {
        video.play();
      }
    },
    onPlay() {
      this.isPlaying = true;
    },
    onPause() {
      this.isPlaying = false;
    }
  }
};
</script>

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

相关文章

vue 实现pdf

vue 实现pdf

在Vue中实现PDF功能 在Vue项目中实现PDF功能通常涉及PDF生成、预览或下载。以下是几种常见实现方式: 使用vue-pdf库预览PDF 安装依赖: npm install vue-pdf…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue实现爬虫

vue实现爬虫

Vue 实现爬虫的基本思路 Vue.js 本身是一个前端框架,主要用于构建用户界面。要实现爬虫功能,通常需要结合后端技术或浏览器自动化工具。以下是几种常见的方法: 方法一:Vue + Node.js…

vue实现导航

vue实现导航

使用 Vue Router 实现导航 Vue Router 是 Vue.js 官方提供的路由管理器,用于构建单页应用(SPA)的导航系统。以下是实现导航的基本步骤。 安装 Vue Router n…