Events
Usage
from npm package
import { Events } from 'xgplayer'
player.on(Events.PlAY, () => {
// TODO
})
from CDN
const Events = window.Player.Events
player.on(Events.PlAY, () => {
// TODO
})
Events from MediaElement
name | value | description |
---|---|---|
EVENTS.LOAD_START | loadstart | The loadstart event is fired when the browser has started to load a resource |
EVENTS.LOADED_DATA | loadeddata | Frame at the current playback position of the media has finished loading; often the first frame |
EVENTS.BUFFERED_CHANGE | bufferedchange | The range of video.buffered has changed |
EVENTS.PLAY | play | Play |
EVENTS.PAUSE | pause | Pause |
EVENTS.ENDED | ended | Ended |
EVENTS.ERROR | error | Error occurs |
EVENTS.AUTOPLAY_PREVENTED | autoplay_was_prevented | Auto play request was prevented by browser |
EVENTS.PLAYING | playing | Playback has resumed from pause or stall |
EVENTS.SEEKING | seeking | A seek operation starts |
EVENTS.SEEKED | seeked | A seek operation finished |
EVENTS.TIME_UPDATE | timeupdate | MediaElement.currentTime attribute has been updated |
EVENTS.WAITING | waiting | Playback has stopped because of a temporary lack of data |
EVENTS.CANPLAY | canplay | The user agent can play the media, and estimates that enough data has been loaded to play the media up to its end without having to stop for further buffering of content |
EVENTS.CANPLAY_THROUGH | canplaythrough | The user agent 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 |
EVENTS.DURATION_CHANGE | durationchange | The duration attribute has been updated |
EVENTS.VOLUME_CHANGE | volumechange | The volume has changed |
EVENTS.RATE_CHANGE | ratechange | Playback rate has changed |
Events from player
name | value | description |
---|---|---|
EVENTS.READY | ready | player was ready for start |
EVENTS.COMPLETE | complete | MediaElement was created, |
EVENTS.URL_CHANGE | urlchange | Player.src was setted for new video resource playback |
EVENTS.PLAYER_FOCUS | focus | User interacting with player UI |
EVENTS.PLAYER_BLUR | blur | User not interacting with player UI |
EVENTS.FULLSCREEN_CHANGE | fullscreen_change | Fullscreen mode changed |
EVENTS.CSS_FULLSCREEN_CHANGE | cssFullscreen_change | Css-fullscreen mode changed |
EVENTS.MINI_STATE_CHANGE | mini_state_change | Mini playback mode changed |
EVENTS.DESTROY | destroy | Player destroyed |
EVENTS.REPLAY | replay | Replay |
EVENTS.RETRY | retry | Playback failed (commonly for network staff) and player retry |
EVENTS.DEFINITION_CHANGE | definition_change | Playback definition changed () |
EVENTS.VIDEO_RESIZE | video_resize | DOM size of player was changed |
EVENTS.PIP_CHANGE | pip_change | Picture in picture mode changed |
EVENTS.ROTATE | rotate | Player was rotated |
EVENTS.SHORTCUT | shortcut | Player builtin shortcut triggered |
EVENTS.SEI_PARSED | SEI_PARSED | SEI info parsed by player(only for xgplayer-flv、xgplayer-hls) |
EVENTS.USER_ACTION | user_action | (3.0.0-alpha.92-2之后支持 ) User interaction with player builtin plugins, for example: user click the play button |
Examples
Fullscreen mode changed
import Player, { Events } from 'xgplayer'
player.on(Events.FULLSCREEN_CHANGE, (isFullscreen) => {
if (isFullscreen) {
// Fullscreen mode entered
} else {
// Fullscreen mode exited
}
})
Playback rate changed
import Player, { Events } from 'xgplayer'
player.on(Events.RATE_CHANGE, () => {
const rate = player.playbackRate // default to 1
})
Volume changed
import Player, { Events } from 'xgplayer'
player.on(Events.VOLUME_CHANGE, () => {
const muted = player.muted // If the player is muted
const volume = player.volume
})
Shortcut
import Player, { Events } from 'xgplayer'
const player = new Player({
...
})
/**
* {
* action: String | Function, // Player action triggered by shortcut
* disable: Boolean, // Player action is disabled
* key: String, // Shortcut key
* keyCode: Number // Shortcut keyCode
* }
*/
player.on(Events.SHORTCUT, ({ action, disabled, key, keyCode }) => {
})
Monitor for user interaction
import Player, { Events } from 'xgplayer'
const player = new Player({
...
})
player.on(Events.USER_ACTION, (data, player) => {
// data structure
/**
* data = {
* action: String, // User action type
* pluginName: String, // name of Plugin which interacted with
* event: Event, // DOM event
* props: [{ // props list witch changed
* props: String, // prop key
* from: any, // value before action
* to: any // value after action
* }],
* currentTime: Number, // MediaElement currentTime
* duration: Number, // MediaElement duration
* ended: Boolean, // If mediaElement is ended
* paused: Boolean, // If mediaElement is paused
* }
*/
})