PlaybackStatsListener


@UnstableApi
class PlaybackStatsListener : AnalyticsListener, PlaybackSessionManager.Listener


AnalyticsListener to gather PlaybackStats from the player.

For accurate measurements, the listener should be added to the player before loading media, i.e., getPlaybackState should be STATE_IDLE.

Playback stats are gathered separately for each playback session, i.e. each window in the Timeline and each single ad.

Summary

Nested types

A listener for PlaybackStats updates.

Public constructors

PlaybackStatsListener(
    keepHistory: Boolean,
    callback: PlaybackStatsListener.Callback?
)

Creates listener for playback stats.

Public functions

PlaybackStats!

Returns the combined PlaybackStats for all playback sessions this listener was and is listening to.

PlaybackStats?

Returns the PlaybackStats for the currently playback session, or null if no session is active.

Unit
onAdPlaybackStarted(
    eventTime: AnalyticsListener.EventTime!,
    contentSessionId: String!,
    adSessionId: String!
)

Called when a session is interrupted by ad playback.

Unit
onBandwidthEstimate(
    eventTime: AnalyticsListener.EventTime!,
    totalLoadTimeMs: Int,
    totalBytesLoaded: Long,
    bitrateEstimate: Long
)

Called when the bandwidth estimate for the current data source has been updated.

Unit
onDownstreamFormatChanged(
    eventTime: AnalyticsListener.EventTime!,
    mediaLoadData: MediaLoadData!
)

Called when the downstream format sent to the renderers changed.

Unit
onDrmSessionManagerError(
    eventTime: AnalyticsListener.EventTime!,
    error: Exception!
)

Called when a drm error occurs.

Unit
onDroppedVideoFrames(
    eventTime: AnalyticsListener.EventTime!,
    droppedFrames: Int,
    elapsedMs: Long
)

Called after video frames have been dropped.

Unit

Called after one or more events occurred.

Unit
onLoadError(
    eventTime: AnalyticsListener.EventTime!,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!,
    error: IOException!,
    wasCanceled: Boolean
)

Called when a media source loading error occurred.

Unit
onPositionDiscontinuity(
    eventTime: AnalyticsListener.EventTime!,
    oldPosition: Player.PositionInfo!,
    newPosition: Player.PositionInfo!,
    @Player.DiscontinuityReason reason: Int
)

Called when a position discontinuity occurred.

Unit
onSessionActive(
    eventTime: AnalyticsListener.EventTime!,
    sessionId: String!
)

Called when a session becomes active, i.e. playing in the foreground.

Unit
onSessionCreated(
    eventTime: AnalyticsListener.EventTime!,
    sessionId: String!
)

Called when a new session is created as a result of updateSessions.

Unit
onSessionFinished(
    eventTime: AnalyticsListener.EventTime!,
    sessionId: String!,
    automaticTransitionToNextPlayback: Boolean
)

Called when a session is permanently finished.

Unit
onVideoSizeChanged(
    eventTime: AnalyticsListener.EventTime!,
    videoSize: VideoSize!
)

Called before a frame is rendered for the first time since setting the surface, and each time there's a change in the size or pixel aspect ratio of the video being rendered.

Inherited Constants

From androidx.media3.exoplayer.analytics.AnalyticsListener
const Int

Audio attributes changed.

const Int

The audio codec encountered an error.

const Int

An audio renderer created a decoder.

const Int

An audio renderer released a decoder.

const Int

An audio renderer was disabled.

const Int

An audio renderer was enabled.

const Int

The format consumed by an audio renderer changed.

const Int

The audio position has increased for the first time since the last pause or position reset.

const Int

An audio session id was set.

const Int

The audio sink encountered a non-fatal error.

const Int

An audio track has been initialized.

const Int

An audio track has been released.

const Int

An audio underrun occurred.

const Int

getAvailableCommands changed.

const Int

The bandwidth estimate has been updated.

const Int

getCurrentCues changed.

const Int

getDeviceInfo changed.

const Int

getDeviceVolume changed.

const Int

The downstream format sent to renderers changed.

const Int

DRM keys were loaded.

const Int

DRM keys were removed.

const Int

DRM keys were restored.

const Int

A DRM session has been acquired.

const Int

A non-fatal DRM session manager error occurred.

const Int

A DRM session has been released.

const Int

Video frames have been dropped.

const Int

isLoading ()} changed.

const Int

isPlaying changed.

const Int

A source canceled loading data.

const Int

A source started completed loading data.

const Int

A source had a non-fatal error loading data.

const Int

A source started loading data.

const Int

getMaxSeekToPreviousPosition changed.

const Int

getCurrentMediaItem changed or the player started repeating the current item.

const Int

getMediaMetadata changed.

const Int

Metadata associated with the current playback time was reported.

const Int

getPlaybackParameters changed.

const Int

getPlaybackState changed.

const Int

getPlaybackSuppressionReason changed.

const Int

getPlayerError changed.

const Int

The player was released.

const Int

getPlaylistMetadata changed.

const Int

getPlayWhenReady changed.

const Int

A position discontinuity occurred.

const Int

The first frame has been rendered since setting the surface, since the renderer was reset or since the stream changed.

const Int

getRepeatMode changed.

const Int

getSeekBackIncrement changed.

const Int

getSeekForwardIncrement changed.

const Int

getShuffleModeEnabled changed.

const Int

Skipping silences was enabled or disabled in the audio stream.

const Int

The surface size changed.

const Int

getCurrentTimeline changed.

const Int

getCurrentTracks changed.

const Int

getTrackSelectionParameters changed.

const Int

Data was removed from the end of the media buffer.

const Int

The video codec encountered an error.

const Int

A video renderer created a decoder.

const Int

A video renderer released a decoder.

const Int

A video renderer was disabled.

const Int

A video renderer was enabled.

const Int

Video frame processing offset data has been reported.

const Int

The format consumed by a video renderer changed.

const Int

The video size changed.

const Int

The volume changed.

Inherited functions

From androidx.media3.exoplayer.analytics.AnalyticsListener
Unit
@UnstableApi
onAudioAttributesChanged(
    eventTime: AnalyticsListener.EventTime!,
    audioAttributes: AudioAttributes!
)

Called when the audio attributes change.

Unit
@UnstableApi
onAudioCodecError(
    eventTime: AnalyticsListener.EventTime!,
    audioCodecError: Exception!
)

Called when an audio decoder encounters an error.

Unit
@UnstableApi
onAudioDecoderInitialized(
    eventTime: AnalyticsListener.EventTime!,
    decoderName: String!,
    initializationDurationMs: Long
)

This function is deprecated.

Use onAudioDecoderInitialized.

Unit
@UnstableApi
onAudioDecoderInitialized(
    eventTime: AnalyticsListener.EventTime!,
    decoderName: String!,
    initializedTimestampMs: Long,
    initializationDurationMs: Long
)

Called when an audio renderer creates a decoder.

Unit
@UnstableApi
onAudioDecoderReleased(
    eventTime: AnalyticsListener.EventTime!,
    decoderName: String!
)

Called when an audio renderer releases a decoder.

Unit
@UnstableApi
onAudioDisabled(
    eventTime: AnalyticsListener.EventTime!,
    decoderCounters: DecoderCounters!
)

Called when an audio renderer is disabled.

Unit
@UnstableApi
onAudioEnabled(
    eventTime: AnalyticsListener.EventTime!,
    decoderCounters: DecoderCounters!
)

Called when an audio renderer is enabled.

Unit

This function is deprecated.

Use onAudioInputFormatChanged.

Unit
@UnstableApi
onAudioInputFormatChanged(
    eventTime: AnalyticsListener.EventTime!,
    format: Format!,
    decoderReuseEvaluation: DecoderReuseEvaluation?
)

Called when the format of the media being consumed by an audio renderer changes.

Unit
@UnstableApi
onAudioPositionAdvancing(
    eventTime: AnalyticsListener.EventTime!,
    playoutStartSystemTimeMs: Long
)

Called when the audio position has increased for the first time since the last pause or position reset.

Unit
@UnstableApi
onAudioSessionIdChanged(
    eventTime: AnalyticsListener.EventTime!,
    audioSessionId: Int
)

Called when the audio session ID changes.

Unit
@UnstableApi
onAudioSinkError(
    eventTime: AnalyticsListener.EventTime!,
    audioSinkError: Exception!
)

Called when AudioSink has encountered an error.

Unit

Called when an AudioTrack has been initialized.

Unit

Called when an AudioTrack has been released.

Unit
@UnstableApi
onAudioUnderrun(
    eventTime: AnalyticsListener.EventTime!,
    bufferSize: Int,
    bufferSizeMs: Long,
    elapsedSinceLastFeedMs: Long
)

Called when an audio underrun occurs.

Unit
@UnstableApi
onAvailableCommandsChanged(
    eventTime: AnalyticsListener.EventTime!,
    availableCommands: Player.Commands!
)

Called when the player's available commands changed.

Unit

Called when there is a change in the CueGroup.

Unit

This function is deprecated.

Use onCues instead.

Unit
@UnstableApi
onDeviceInfoChanged(
    eventTime: AnalyticsListener.EventTime!,
    deviceInfo: DeviceInfo!
)

Called when the device information changes

Unit
@UnstableApi
onDeviceVolumeChanged(
    eventTime: AnalyticsListener.EventTime!,
    volume: Int,
    muted: Boolean
)

Called when the device volume or mute state changes.

Unit

Called each time drm keys are loaded.

Unit

Called each time offline drm keys are removed.

Unit

Called each time offline drm keys are restored.

Unit

This function is deprecated.

Implement onDrmSessionAcquired instead.

Unit

Called each time a drm session is acquired.

Unit

Called each time a drm session is released.

Unit
@UnstableApi
onIsLoadingChanged(
    eventTime: AnalyticsListener.EventTime!,
    isLoading: Boolean
)

Called when the player starts or stops loading data from a source.

Unit
@UnstableApi
onIsPlayingChanged(
    eventTime: AnalyticsListener.EventTime!,
    isPlaying: Boolean
)

Called when the player starts or stops playing.

Unit
@UnstableApi
onLoadCanceled(
    eventTime: AnalyticsListener.EventTime!,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!
)

Called when a media source canceled loading data.

Unit
@UnstableApi
onLoadCompleted(
    eventTime: AnalyticsListener.EventTime!,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!
)

Called when a media source completed loading data.

Unit
@UnstableApi
onLoadStarted(
    eventTime: AnalyticsListener.EventTime!,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!
)

Called when a media source started loading data.

Unit
@UnstableApi
onLoadingChanged(
    eventTime: AnalyticsListener.EventTime!,
    isLoading: Boolean
)

This function is deprecated.

Use onIsLoadingChanged instead.

Unit
@UnstableApi
onMaxSeekToPreviousPositionChanged(
    eventTime: AnalyticsListener.EventTime!,
    maxSeekToPreviousPositionMs: Long
)

Called when the maximum position for which seekToPrevious seeks to the previous window changes.

Unit

Called when playback transitions to a different media item.

Unit
@UnstableApi
onMediaMetadataChanged(
    eventTime: AnalyticsListener.EventTime!,
    mediaMetadata: MediaMetadata!
)

Called when the combined MediaMetadata changes.

Unit

Called when there is Metadata associated with the current playback time.

Unit
@UnstableApi
onPlayWhenReadyChanged(
    eventTime: AnalyticsListener.EventTime!,
    playWhenReady: Boolean,
    @Player.PlayWhenReadyChangeReason reason: Int
)

Called when the value changed that indicates whether playback will proceed when ready.

Unit

Called when the playback parameters changed.

Unit

Called when the playback state changed.

Unit

Called when playback suppression reason changed.

Unit

Called when a fatal player error occurred.

Unit

Called when the PlaybackException returned by getPlayerError changes.

Unit

Called when the Player is released.

Unit
@UnstableApi
onPlayerStateChanged(
    eventTime: AnalyticsListener.EventTime!,
    playWhenReady: Boolean,
    @Player.State playbackState: Int
)

This function is deprecated.

Use onPlaybackStateChanged and onPlayWhenReadyChanged instead.

Unit
@UnstableApi
onPlaylistMetadataChanged(
    eventTime: AnalyticsListener.EventTime!,
    playlistMetadata: MediaMetadata!
)

Called when the playlist MediaMetadata changes.

Unit

This function is deprecated.

Use onPositionDiscontinuity instead.

Unit
@UnstableApi
onRenderedFirstFrame(
    eventTime: AnalyticsListener.EventTime!,
    output: Any!,
    renderTimeMs: Long
)

Called when a frame is rendered for the first time since setting the surface, or since the renderer was reset, or since the stream being rendered was changed.

Unit

Called when the repeat mode changed.

Unit
@UnstableApi
onSeekBackIncrementChanged(
    eventTime: AnalyticsListener.EventTime!,
    seekBackIncrementMs: Long
)

Called when the seek back increment changed.

Unit
@UnstableApi
onSeekForwardIncrementChanged(
    eventTime: AnalyticsListener.EventTime!,
    seekForwardIncrementMs: Long
)

Called when the seek forward increment changed.

Unit

This function is deprecated.

Use onPositionDiscontinuity instead, listening to changes with DISCONTINUITY_REASON_SEEK.

Unit
@UnstableApi
onShuffleModeChanged(
    eventTime: AnalyticsListener.EventTime!,
    shuffleModeEnabled: Boolean
)

Called when the shuffle mode changed.

Unit
@UnstableApi
onSkipSilenceEnabledChanged(
    eventTime: AnalyticsListener.EventTime!,
    skipSilenceEnabled: Boolean
)

Called when skipping silences is enabled or disabled in the audio stream.

Unit
@UnstableApi
onSurfaceSizeChanged(
    eventTime: AnalyticsListener.EventTime!,
    width: Int,
    height: Int
)

Called when the output surface size changed.

Unit

Called when the timeline changed.

Unit

Called when track selection parameters change.

Unit

Called when the tracks change.

Unit
@UnstableApi
onUpstreamDiscarded(
    eventTime: AnalyticsListener.EventTime!,
    mediaLoadData: MediaLoadData!
)

Called when data is removed from the back of a media buffer, typically so that it can be re-buffered in a different format.

Unit
@UnstableApi
onVideoCodecError(
    eventTime: AnalyticsListener.EventTime!,
    videoCodecError: Exception!
)

Called when a video decoder encounters an error.

Unit
@UnstableApi
onVideoDecoderInitialized(
    eventTime: AnalyticsListener.EventTime!,
    decoderName: String!,
    initializationDurationMs: Long
)

This function is deprecated.

Use onVideoDecoderInitialized.

Unit
@UnstableApi
onVideoDecoderInitialized(
    eventTime: AnalyticsListener.EventTime!,
    decoderName: String!,
    initializedTimestampMs: Long,
    initializationDurationMs: Long
)

Called when a video renderer creates a decoder.

Unit
@UnstableApi
onVideoDecoderReleased(
    eventTime: AnalyticsListener.EventTime!,
    decoderName: String!
)

Called when a video renderer releases a decoder.

Unit
@UnstableApi
onVideoDisabled(
    eventTime: AnalyticsListener.EventTime!,
    decoderCounters: DecoderCounters!
)

Called when a video renderer is disabled.

Unit
@UnstableApi
onVideoEnabled(
    eventTime: AnalyticsListener.EventTime!,
    decoderCounters: DecoderCounters!
)

Called when a video renderer is enabled.

Unit
@UnstableApi
onVideoFrameProcessingOffset(
    eventTime: AnalyticsListener.EventTime!,
    totalProcessingOffsetUs: Long,
    frameCount: Int
)

Called when there is an update to the video frame processing offset reported by a video renderer.

Unit

This function is deprecated.

Use onVideoInputFormatChanged.

Unit
@UnstableApi
onVideoInputFormatChanged(
    eventTime: AnalyticsListener.EventTime!,
    format: Format!,
    decoderReuseEvaluation: DecoderReuseEvaluation?
)

Called when the format of the media being consumed by a video renderer changes.

Unit
@UnstableApi
onVideoSizeChanged(
    eventTime: AnalyticsListener.EventTime!,
    width: Int,
    height: Int,
    unappliedRotationDegrees: Int,
    pixelWidthHeightRatio: Float
)

This function is deprecated.

Implement onVideoSizeChanged instead.

Unit

Called when the volume changes.

Public constructors

PlaybackStatsListener

PlaybackStatsListener(
    keepHistory: Boolean,
    callback: PlaybackStatsListener.Callback?
)

Creates listener for playback stats.

Parameters
keepHistory: Boolean

Whether the reported PlaybackStats should keep the full history of events.

callback: PlaybackStatsListener.Callback?

An optional callback for finished PlaybackStats.

Public functions

getCombinedPlaybackStats

fun getCombinedPlaybackStats(): PlaybackStats!

Returns the combined PlaybackStats for all playback sessions this listener was and is listening to.

Note that these PlaybackStats will not contain the full history of events.

Returns
PlaybackStats!

The combined PlaybackStats for all playback sessions.

getPlaybackStats

fun getPlaybackStats(): PlaybackStats?

Returns the PlaybackStats for the currently playback session, or null if no session is active.

Returns
PlaybackStats?

PlaybackStats for the current playback session.

onAdPlaybackStarted

fun onAdPlaybackStarted(
    eventTime: AnalyticsListener.EventTime!,
    contentSessionId: String!,
    adSessionId: String!
): Unit

Called when a session is interrupted by ad playback.

Parameters
eventTime: AnalyticsListener.EventTime!

The EventTime at which the ad playback starts.

contentSessionId: String!

The session identifier of the content session.

adSessionId: String!

The identifier of the ad session.

onBandwidthEstimate

fun onBandwidthEstimate(
    eventTime: AnalyticsListener.EventTime!,
    totalLoadTimeMs: Int,
    totalBytesLoaded: Long,
    bitrateEstimate: Long
): Unit

Called when the bandwidth estimate for the current data source has been updated.

Parameters
eventTime: AnalyticsListener.EventTime!

The event time.

totalLoadTimeMs: Int

The total time spend loading this update is based on, in milliseconds.

totalBytesLoaded: Long

The total bytes loaded this update is based on.

bitrateEstimate: Long

The bandwidth estimate, in bits per second.

onDownstreamFormatChanged

fun onDownstreamFormatChanged(
    eventTime: AnalyticsListener.EventTime!,
    mediaLoadData: MediaLoadData!
): Unit

Called when the downstream format sent to the renderers changed.

Parameters
eventTime: AnalyticsListener.EventTime!

The event time.

mediaLoadData: MediaLoadData!

The MediaLoadData defining the newly selected media data.

onDrmSessionManagerError

fun onDrmSessionManagerError(
    eventTime: AnalyticsListener.EventTime!,
    error: Exception!
): Unit

Called when a drm error occurs.

This method being called does not indicate that playback has failed, or that it will fail. The player may be able to recover from the error. Hence applications should not implement this method to display a user visible error or initiate an application level retry. onPlayerError is the appropriate place to implement such behavior. This method is called to provide the application with an opportunity to log the error if it wishes to do so.

Parameters
eventTime: AnalyticsListener.EventTime!

The event time.

error: Exception!

The error.

onDroppedVideoFrames

fun onDroppedVideoFrames(
    eventTime: AnalyticsListener.EventTime!,
    droppedFrames: Int,
    elapsedMs: Long
): Unit

Called after video frames have been dropped.

Parameters
eventTime: AnalyticsListener.EventTime!

The event time.

droppedFrames: Int

The number of dropped frames since the last call to this method.

elapsedMs: Long

The duration in milliseconds over which the frames were dropped. This duration is timed from when the renderer was started or from when dropped frames were last reported (whichever was more recent), and not from when the first of the reported drops occurred.

onEvents

fun onEvents(player: Player!, events: AnalyticsListener.Events!): Unit

Called after one or more events occurred.

State changes and events that happen within one Looper message queue iteration are reported together and only after all individual callbacks were triggered.

Listeners should prefer this method over individual callbacks in the following cases:

Parameters
player: Player!

The Player.

events: AnalyticsListener.Events!

The Events that occurred in this iteration.

onLoadError

fun onLoadError(
    eventTime: AnalyticsListener.EventTime!,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!,
    error: IOException!,
    wasCanceled: Boolean
): Unit

Called when a media source loading error occurred.

This method being called does not indicate that playback has failed, or that it will fail. The player may be able to recover from the error. Hence applications should not implement this method to display a user visible error or initiate an application level retry. onPlayerError is the appropriate place to implement such behavior. This method is called to provide the application with an opportunity to log the error if it wishes to do so.

Parameters
eventTime: AnalyticsListener.EventTime!

The event time.

loadEventInfo: LoadEventInfo!

The LoadEventInfo defining the load event.

mediaLoadData: MediaLoadData!

The MediaLoadData defining the data being loaded.

error: IOException!

The load error.

wasCanceled: Boolean

Whether the load was canceled as a result of the error.

onPositionDiscontinuity

fun onPositionDiscontinuity(
    eventTime: AnalyticsListener.EventTime!,
    oldPosition: Player.PositionInfo!,
    newPosition: Player.PositionInfo!,
    @Player.DiscontinuityReason reason: Int
): Unit

Called when a position discontinuity occurred.

Parameters
eventTime: AnalyticsListener.EventTime!

The event time.

oldPosition: Player.PositionInfo!

The position before the discontinuity.

newPosition: Player.PositionInfo!

The position after the discontinuity.

@Player.DiscontinuityReason reason: Int

The reason for the position discontinuity.

onSessionActive

fun onSessionActive(
    eventTime: AnalyticsListener.EventTime!,
    sessionId: String!
): Unit

Called when a session becomes active, i.e. playing in the foreground.

Parameters
eventTime: AnalyticsListener.EventTime!

The EventTime at which the session becomes active.

sessionId: String!

The identifier of the session.

onSessionCreated

fun onSessionCreated(
    eventTime: AnalyticsListener.EventTime!,
    sessionId: String!
): Unit

Called when a new session is created as a result of updateSessions.

Parameters
eventTime: AnalyticsListener.EventTime!

The EventTime at which the session is created.

sessionId: String!

The identifier of the new session.

onSessionFinished

fun onSessionFinished(
    eventTime: AnalyticsListener.EventTime!,
    sessionId: String!,
    automaticTransitionToNextPlayback: Boolean
): Unit

Called when a session is permanently finished.

Parameters
eventTime: AnalyticsListener.EventTime!

The EventTime at which the session finished.

sessionId: String!

The identifier of the finished session.

automaticTransitionToNextPlayback: Boolean

Whether the session finished because of an automatic transition to the next playback item.

onVideoSizeChanged

fun onVideoSizeChanged(
    eventTime: AnalyticsListener.EventTime!,
    videoSize: VideoSize!
): Unit

Called before a frame is rendered for the first time since setting the surface, and each time there's a change in the size or pixel aspect ratio of the video being rendered.

Parameters
eventTime: AnalyticsListener.EventTime!

The event time.

videoSize: VideoSize!

The new size of the video.