You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vue项目中为Video.js播放器添加事件监听器?

Adding Video.js Event Listeners in Vue

Hey there! I notice you've integrated Video.js into your Vue app but aren't sure how to add event listeners for play, pause, and stop/ended events. Let's fix that right up—here's exactly what you need to do.

1. Add Listeners in the Player Ready Callback

The key is to attach your event listeners inside the ready callback you already have—this ensures the player is fully initialized before you try to interact with it. Video.js uses standard, intuitive event names for common actions:

  • play: Triggers when the video starts playing
  • pause: Triggers when the video is paused
  • ended: Triggers when the video finishes playing (this is the "stop" event you're looking for)

Here's how to modify your existing code:

this.player = videojs(this.$refs.videoPlayer, this.options, () => {
  this.player.log('onPlayerReady', this);

  // Listen for play event
  this.player.on('play', () => {
    console.log('Video started playing!');
    // Add your custom logic here (e.g., update a play state variable)
  });

  // Listen for pause event
  this.player.on('pause', () => {
    console.log('Video paused!');
  });

  // Listen for end (stop) event
  this.player.on('ended', () => {
    console.log('Video finished playing!');
  });
});

2. Clean Up Listeners (Critical for Vue!)

Vue components mount and unmount dynamically, so you need to remove event listeners and dispose of the Video.js instance when your component is destroyed to avoid memory leaks and unexpected behavior.

Add this lifecycle hook to your component:

beforeUnmount() {
  if (this.player) {
    // Remove each event listener
    this.player.off('play');
    this.player.off('pause');
    this.player.off('ended');
    // Dispose of the player instance entirely
    this.player.dispose();
    this.player = null;
  }
}

3. Full Component Example (Best Practice)

For better code organization, it's a good idea to extract your event handlers into separate methods. Here's a complete Vue component example:

<template>
  <div>
    <video ref="videoPlayer" class="video-js vjs-big-play-centered"></video>
  </div>
</template>

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

export default {
  data() {
    return {
      player: null,
      playerOptions: {
        autoplay: false,
        controls: true,
        responsive: true,
        sources: [
          {
            src: 'https://example.com/your-video.mp4',
            type: 'video/mp4'
          }
        ]
      }
    };
  },
  mounted() {
    this.initVideoPlayer();
  },
  beforeUnmount() {
    this.cleanupVideoPlayer();
  },
  methods: {
    initVideoPlayer() {
      this.player = videojs(this.$refs.videoPlayer, this.playerOptions, () => {
        this.player.log('Player is ready!');
        
        // Attach listeners to component methods
        this.player.on('play', this.handlePlay);
        this.player.on('pause', this.handlePause);
        this.player.on('ended', this.handleEnded);
      });
    },
    handlePlay() {
      console.log('Video is now playing');
      // Example: Update component state
      // this.isPlaying = true;
    },
    handlePause() {
      console.log('Video is paused');
      // this.isPlaying = false;
    },
    handleEnded() {
      console.log('Video has stopped (ended)');
      // this.isPlaying = false;
    },
    cleanupVideoPlayer() {
      if (this.player) {
        // Remove listeners with their corresponding methods
        this.player.off('play', this.handlePlay);
        this.player.off('pause', this.handlePause);
        this.player.off('ended', this.handleEnded);
        // Destroy the player
        this.player.dispose();
        this.player = null;
      }
    }
  }
};
</script>

<style scoped>
.video-js {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
}
</style>

Quick Notes

  • If you need other events (like time updates or volume changes), most follow intuitive naming conventions—you can find the full list in Video.js's internal event references.
  • Never skip cleaning up the player instance; this prevents leftover DOM elements and memory leaks that can slow down your app over time.

内容的提问来源于stack exchange,提问作者Yaroslav Saenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 20:57:39