Skip to main content

实例事件

播放器的事件分为两种,一种视频的原生事件,另外一种是自定义的事件。原生事件来自 video 元素,是直接监听video元素的原始事件,为了让它和自定义事件区分,原生事件添加了前缀 video:

原生事件

更多信息请阅读 Mozilla Video Events

事件名字触发时间
video:canplayThe browser can play the media, but estimates that not enough data has been loaded to play the media up to its end without having to stop for further buffering of content.
video:canplaythroughThe browser estimates it can play the media up to its end without stopping for content buffering.
video:completeThe rendering of an OfflineAudioContext is terminated.
video:durationchangeThe duration attribute has been updated.
video:emptiedThe media has become empty; for example, this event is sent if the media has already been loaded (or partially loaded), and the load() method is called to reload it.
video:endedPlayback has stopped because the end of the media was reached.
video:loadeddataThe first frame of the media has finished loading.
video:loadedmetadataThe metadata has been loaded.
video:pausePlayback has been paused.
video:playPlayback has begun.
video:playingPlayback is ready to start after having been paused or delayed due to lack of data.
video:progressFired periodically as the browser loads a resource.
video:ratechangeThe playback rate has changed.
video:seekedA seek operation completed.
video:seekingA seek operation began.
video:stalledThe user agent is trying to fetch media data, but data is unexpectedly not forthcoming.
video:suspendMedia data loading has been suspended.
video:timeupdateThe time indicated by the currentTime attribute has been updated.
video:volumechangeThe volume has changed.
video:waitingPlayback has stopped because of a temporary lack of data
▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('video:play', (...args) => {
console.info(args);
});

自定义事件

ready

当实例初始化完成,并视频可以被播放时触发,只会触发一个,哪怕视频地址变化了

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('ready', (...args) => {
console.info(args);
});

play

在视频被播放后触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('play', (...args) => {
console.info(args);
});

pause

在视频被暂停后触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('pause', (...args) => {
console.info(args);
});

seek

在视频进行时间跳转时触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('seek', (...args) => {
console.info(args);
});

art.on('ready', (...args) => {
art.seek = 5;
});
提示

设置 art.currentTime 时不会触发 seek 事件

volume

在视频音量变化时触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('volume', (...args) => {
console.info(args);
});

art.on('ready', (...args) => {
art.volume = 0.5;
});

destroy

在播放器实例被销毁时触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('destroy', (...args) => {
console.info(args);
});

art.on('ready', (...args) => {
art.destroy();
});

focus

在播放器获得焦点时触发,如点击了播放器

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('focus', (...args) => {
console.info(args);
});

blur

在播放器失去焦点时触发,如点击播放器以外的区域

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('blur', (...args) => {
console.info(args);
});

hover

在鼠标移动进播放器时触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('hover', (state) => {
// state 为true时,鼠标从外面移进播放器
// state 为false时,鼠标从播放器移出外面
console.info(state);
});

resize

在播放器尺寸变化时触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
autoSize: true,
});

art.on('resize', (...args) => {
console.info(args);
});

mousemove

在鼠标经过播放器时触发,和hover不一样,它会触发多次

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('mousemove', (...args) => {
console.info(args);
});

url

在视频地址变化时触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('url', (...args) => {
console.info(args);
});

art.on('ready', (...args) => {
art.url = '/assets/sample/video.mp4?t=0';
});

fullscreen

在播放器窗口全屏时触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
fullscreen: true,
});

art.on('fullscreen', (...args) => {
console.info(args);
});

fullscreenWeb

在播放器网页全屏时触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
fullscreenWeb: true,
});

art.on('fullscreenWeb', (...args) => {
console.info(args);
});

hotkey

在快捷键被使用时触发

▶ Run Code
var art = new Artplayer({
container: '.artplayer-app',
url: '/assets/sample/video.mp4',
});

art.on('hotkey', (...args) => {
console.info(args);
});