Ticket #1424 (closed feature: fixed)
onBeforePlay() event
| Reported by: | pablo | Owned by: | |
|---|---|---|---|
| Priority: | High | Milestone: | Player 5.9 |
| Component: | general | Keywords: | |
| Cc: | Forum thread: |
Description (last modified by pablo) (diff)
There are several use cases where it is useful to be able to respond to an event which is fired before the player begins playing a media item. A listener may wish to lock the player and perform an action before the media is loaded, record some analytics data, play a pre-roll ad, or prevent the player from playing entirely by stopping the player.
Typical player event event flow for a single-item playlist should be:
- onReady
- onPlaylist
- onPlaylistItem
- onBeforePlay
- onBuffer (oldstate: "IDLE")
- onBufferFull
- onPlay (oldstate: "BUFFERING")
- onComplete
- onIdle (oldstate: "PLAYING")
Flash Plugin API
Flash plugins wishing to listen for this event should add an event listener for the following event:
MediaEvent.JWPLAYER_MEDIA_BEFOREPLAY (jwplayerMediaBeforePlay)
JavaScript API
Setup block:
jwplayer().setup({
...,
events: {
onBeforePlay: function(evt) {
// Event handler
}
}
});
Post-setup or JavaScript plugin:
player.onBeforePlay(eventHandler);
Examples
JavaScript API
Prevent a file from playing:
jwplayer('player').setup({
...
events: {
onBeforePlay: function(evt) {
if (shouldStopPlayer) {
this.stop();
}
}
}
});
Flash Plugin API
Alter the current playlist item before playing:
player.addEventListener(MediaEvent.JWPLAYER_MEDIA_BEFOREPLAY,
function(event:MediaEvent):void {
player.playlist.currentItem.file = 'video2.mp4'
}
);
