Skip to the content.

Docs | Android

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

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:

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

Additional User actions

Track change

Rate and Volume change

Errors

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;
}

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, responseCoderesponseMessageheaderFields

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

Ad Events

Defined in AdEvent class.

Code Samples