当前位置:首页 > VUE

vue实现视频播放暂停

2026-02-21 02:49:35VUE

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

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

<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 变量,根据当前状态调用对应方法。

<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
分享给朋友:

相关文章

vue实现中台

vue实现中台

Vue 实现中台系统的关键步骤 技术选型与基础搭建 使用 Vue 3(Composition API)或 Vue 2(Options API)作为前端框架,搭配 Vue Router 实现路由管理,V…

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实现…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind…

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…

vue实现上传

vue实现上传

Vue 文件上传实现方法 使用 Vue 实现文件上传可以通过原生 HTML5 的 <input type="file"> 元素结合 Vue 的事件处理和数据绑定来实现。 基础实现步骤…