Events
The player and some of the plugins fire events that tell the application (and other plugins) about events that occur before/during/after playback.
On this page:
- Listening to events from applications
- Removing Event Listeners from application (v3.6.2 and up)
- Listening to events from plugins
- Core Player Events
- Ad Events
- Code Samples
Listening to events from applications
Call the following Player method, one or more times. eventTypes
is the list of events that should be sent to the given listener:
player.addEventListener(PKEvent.Listener listener, Enum... eventTypes)
Removing Event Listeners from application (v3.6.2 and up)
Starting with PlayKit v3.6.2, the addEventListener method returns the listener that was passed to it. This makes it easier to use it with listeners that are defined by anonymous classes, in the case the application wants to remove them later.
There are two methods that allow removing event listeners:
- Remove listener from specific events:
player.removeEventListener(@NonNull PKEvent.Listener listener, Enum... events); // remove by event
- Remove listener from all events:
player.removeListener(@NonNull PKEvent.Listener listener); // remove all rgeisterd events
Listening to events from plugins
player.addEventListener()
is only meant to be used by applications. It does not work for plugins. Instead, plugins are given an instance of PlayKit’s MessageBus.
messageBus.listen(PKEvent.Listener listener, Enum... eventTypes)
Core Player Events
The Player events are defined in the PlayerEvent class.
Normal Flow
- SOURCE_SELECTED: Sent when a playback source is selected
- LOADED_METADATA: The media’s metadata has finished loading; all attributes now contain as much useful information as they’re going to.
- DURATION_CHANGE: The metadata has loaded or changed, indicating a change in duration of the media. This is sent, for example, when the media has loaded enough that the duration is known.
- TRACKS_AVAILABLE: Sent when track info is available.
- PLAYBACK_INFO_UPDATED: Sent event that notify about changes in the playback parameters. When bitrate of the video or audio track changes or new media loaded. Holds the PlaybackInfo.java object with relevant data.
- CAN_PLAY: Sent when enough data is available that the media can be played, at least for a couple of frames. This corresponds to the HAVE_ENOUGH_DATA readyState.
- PLAY: Sent when playback of the media starts after having been paused; that is, when playback is resumed after a prior pause event.
- PLAYING: Sent when the media begins to play (either for the first time, after having been paused, or after ending and then restarting).
- PLAYHEAD_UPDATED: Send player position every 100 Milisec
- ENDED: Sent when playback completes.
Additional User actions
- PAUSE: Sent when playback is paused.
- SEEKED: Sent when a seek operation completes.
- SEEKING: Sent when a seek operation begins.
- REPLAY:Sent when replay happened.
- STOPPED: sent when stop player api is called
Track change
- VIDEO_TRACK_CHANGED: A video track was selected
- AUDIO_TRACK_CHANGED: An audio track was selected
- TEXT_TRACK_CHANGED: A text track was selected
Rate and Volume change
- PLAYBACK_RATE_CHANGED
- VOLUME_CHANGED: Sent when volume is changed.
Metadata (ID3 tags and related)
- METADATA_AVAILABLE: Sent when there is metadata available for this entry.
Errors
- ERROR: Sent when an error occurs. The element’s error attribute contains more information. Following snippet can be used,
private String getFullPlayerError(PKEvent event) {
try {
PlayerEvent.Error exceptionInfo = (PlayerEvent.Error) event;
PKError playerError = exceptionInfo.error;
Exception playerErrorException = (Exception) playerError.exception;
String errorMetadata = "Player error occurred";
String exceptionClass = "";
String exceptionCause = "";
if (playerErrorException != null && playerErrorException.getCause() != null && playerErrorException.getCause().getClass() != null) {
exceptionClass = playerErrorException.getCause().getClass().getName();
errorMetadata = (playerErrorException.getCause().toString() != null) ? playerErrorException.getCause().toString() : errorMetadata;
} else {
exceptionCause = exceptionInfo.error.errorType.name() + " - " + exceptionInfo.error.message;
}
if (playerErrorException.getCause() instanceof HttpDataSource.InvalidResponseCodeException) {
log.d("InvalidResponseCodeException " + ((HttpDataSource.InvalidResponseCodeException)playerErrorException.getCause()).responseCode);
}
if (playerErrorException.getCause() instanceof UnknownHostException) {
log.d("UnknownHostException");
}
return exceptionCause + " - " + exceptionClass + " - " + errorMetadata;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Client application should also check PKPlayerErrorType
using instanceof
.
PKPlayerErrorType mPlayerErrorType;
PlayerEvent.Error exceptionInfo = (PlayerEvent.Error) event;
if (exceptionInfo != null && exceptionInfo.error != null && exceptionInfo.error.errorType instanceof PKPlayerErrorType) {
mPlayerErrorType = (PKPlayerErrorType) exceptionInfo.error.errorType;
}
SOURCE_ERROR(7000)
- The error occured loading data from MediaSource.RENDERER_ERROR(7001)
- The error occured in a renderer.UNEXPECTED(7002)
- If in runtime any unexpected error occurs.SOURCE_SELECTION_FAILED(7003)
- The error occured to get the source from SourceSelector.FAILED_TO_INITIALIZE_PLAYER(7004)
- The error occured when failed to initilize PlayerEngine.DRM_ERROR(7005)
- In case device does not support widevine modular or license is expired.TRACK_SELECTION_FAILED(7006)
- The error occured if track selection is not possible in TrackSelectionHelper.LOAD_ERROR(7007)
- In case, media is not loaded in any of the MediaSource.
There are more HttpDataSource exceptions which client application should handle.
######Example:
playerErrorException.getCause() instanceof HttpDataSource.InvalidResponseCodeException
Following fields can be extracted from the response object,
responseCode
responseMessage
headerFields
InvalidContentTypeException
- Thrown when the content type is invalid.InvalidResponseCodeException
- Thrown when an attempt to open a connection results in a response code not in the 2xx range.
State Change
The Player can be in one of 4 playback states: IDLE, LOADING, READY, BUFFERING The STATE_CHANGED event is fired when the player transitions between states.
Subtitle Style update
- SUBTITLE_STYLE_CHANGED: Sent when subtitle style is changed
Ad Events
Defined in AdEvent class.
- AD_REQUESTED
- STARTED
- AD_DISPLAYED_AFTER_CONTENT_PAUSE
- PAUSED
- RESUMED
- COMPLETED
- FIRST_QUARTILE
- MIDPOINT
- THIRD_QUARTILE
- SKIPPED
- CLICKED
- TAPPED
- ICON_TAPPED
- AD_BREAK_READY
- AD_PROGRESS
- AD_BREAK_STARTED
- AD_BREAK_ENDED
- AD_BREAK_IGNORED
- CUEPOINTS_CHANGED
- PLAY_HEAD_CHANGED
- LOADED
- CONTENT_PAUSE_REQUESTED
- CONTENT_RESUME_REQUESTED
- ALL_ADS_COMPLETED
- AD_LOAD_TIMEOUT_TIMER_STARTED
- ERROR