MediaMetricsListener


@UnstableApi
@RequiresApi(value = 31)
public final class MediaMetricsListener implements AnalyticsListener, PlaybackSessionManager.Listener


An AnalyticsListener that interacts with the Android MediaMetricsManager.

It listens to playback events and forwards them to a PlaybackSession. The of the playback session can be obtained with getLogSessionId.

Summary

Public methods

static @Nullable MediaMetricsListener
create(Context context)

Creates a media metrics listener.

LogSessionId

Returns the LogSessionId used by this listener.

void
onAdPlaybackStarted(
    AnalyticsListener.EventTime eventTime,
    String contentSessionId,
    String adSessionId
)

Called when a session is interrupted by ad playback.

void
onBandwidthEstimate(
    AnalyticsListener.EventTime eventTime,
    int totalLoadTimeMs,
    long totalBytesLoaded,
    long bitrateEstimate
)

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

void
onDownstreamFormatChanged(
    AnalyticsListener.EventTime eventTime,
    MediaLoadData mediaLoadData
)

Called when the downstream format sent to the renderers changed.

void

Called after one or more events occurred.

void
onLoadError(
    AnalyticsListener.EventTime eventTime,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData,
    IOException error,
    boolean wasCanceled
)

Called when a media source loading error occurred.

void

Called when a fatal player error occurred.

void
onPositionDiscontinuity(
    AnalyticsListener.EventTime eventTime,
    Player.PositionInfo oldPosition,
    Player.PositionInfo newPosition,
    @Player.DiscontinuityReason int reason
)

Called when a position discontinuity occurred.

void
onSessionActive(
    AnalyticsListener.EventTime eventTime,
    String sessionId
)

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

void
onSessionCreated(
    AnalyticsListener.EventTime eventTime,
    String sessionId
)

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

void
onSessionFinished(
    AnalyticsListener.EventTime eventTime,
    String sessionId,
    boolean automaticTransitionToNextPlayback
)

Called when a session is permanently finished.

void
onVideoDisabled(
    AnalyticsListener.EventTime eventTime,
    DecoderCounters decoderCounters
)

Called when a video renderer is disabled.

void
onVideoSizeChanged(
    AnalyticsListener.EventTime 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
static final int

Audio attributes changed.

static final int

The audio codec encountered an error.

static final int

An audio renderer created a decoder.

static final int

An audio renderer released a decoder.

static final int

An audio renderer was disabled.

static final int

An audio renderer was enabled.

static final int

The format consumed by an audio renderer changed.

static final int

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

static final int

An audio session id was set.

static final int

The audio sink encountered a non-fatal error.

static final int

An audio track has been initialized.

static final int

An audio track has been released.

static final int

An audio underrun occurred.

static final int

getAvailableCommands changed.

static final int

The bandwidth estimate has been updated.

static final int

getCurrentCues changed.

static final int

getDeviceInfo changed.

static final int

getDeviceVolume changed.

static final int

The downstream format sent to renderers changed.

static final int

DRM keys were loaded.

static final int

DRM keys were removed.

static final int

DRM keys were restored.

static final int

A DRM session has been acquired.

static final int

A non-fatal DRM session manager error occurred.

static final int

A DRM session has been released.

static final int

Video frames have been dropped.

static final int

isLoading ()} changed.

static final int

isPlaying changed.

static final int

A source canceled loading data.

static final int

A source started completed loading data.

static final int

A source had a non-fatal error loading data.

static final int

A source started loading data.

static final int

getMaxSeekToPreviousPosition changed.

static final int

getCurrentMediaItem changed or the player started repeating the current item.

static final int

getMediaMetadata changed.

static final int

Metadata associated with the current playback time was reported.

static final int

getPlaybackParameters changed.

static final int

getPlaybackState changed.

static final int

getPlaybackSuppressionReason changed.

static final int

getPlayerError changed.

static final int

The player was released.

static final int

getPlaylistMetadata changed.

static final int

getPlayWhenReady changed.

static final int

A position discontinuity occurred.

static final int

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

static final int

A renderer changed its readiness for playback.

static final int

getRepeatMode changed.

static final int

getSeekBackIncrement changed.

static final int

getSeekForwardIncrement changed.

static final int

getShuffleModeEnabled changed.

static final int

Skipping silences was enabled or disabled in the audio stream.

static final int

The surface size changed.

static final int

getCurrentTimeline changed.

static final int

getCurrentTracks changed.

static final int

getTrackSelectionParameters changed.

static final int

Data was removed from the end of the media buffer.

static final int

The video codec encountered an error.

static final int

A video renderer created a decoder.

static final int

A video renderer released a decoder.

static final int

A video renderer was disabled.

static final int

A video renderer was enabled.

static final int

Video frame processing offset data has been reported.

static final int

The format consumed by a video renderer changed.

static final int

The video size changed.

static final int

The volume changed.

Inherited methods

From androidx.media3.exoplayer.analytics.AnalyticsListener
void

Called when the audio attributes change.

void
@UnstableApi
onAudioCodecError(
    AnalyticsListener.EventTime eventTime,
    Exception audioCodecError
)

Called when an audio decoder encounters an error.

void
@UnstableApi
onAudioDecoderInitialized(
    AnalyticsListener.EventTime eventTime,
    String decoderName,
    long initializationDurationMs
)

This method is deprecated.

Use onAudioDecoderInitialized.

void
@UnstableApi
onAudioDecoderInitialized(
    AnalyticsListener.EventTime eventTime,
    String decoderName,
    long initializedTimestampMs,
    long initializationDurationMs
)

Called when an audio renderer creates a decoder.

void
@UnstableApi
onAudioDecoderReleased(
    AnalyticsListener.EventTime eventTime,
    String decoderName
)

Called when an audio renderer releases a decoder.

void
@UnstableApi
onAudioDisabled(
    AnalyticsListener.EventTime eventTime,
    DecoderCounters decoderCounters
)

Called when an audio renderer is disabled.

void
@UnstableApi
onAudioEnabled(
    AnalyticsListener.EventTime eventTime,
    DecoderCounters decoderCounters
)

Called when an audio renderer is enabled.

void
@UnstableApi
onAudioInputFormatChanged(
    AnalyticsListener.EventTime eventTime,
    Format format,
    @Nullable DecoderReuseEvaluation decoderReuseEvaluation
)

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

void
@UnstableApi
onAudioPositionAdvancing(
    AnalyticsListener.EventTime eventTime,
    long playoutStartSystemTimeMs
)

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

void
@UnstableApi
onAudioSessionIdChanged(
    AnalyticsListener.EventTime eventTime,
    int audioSessionId
)

Called when the audio session ID changes.

void
@UnstableApi
onAudioSinkError(
    AnalyticsListener.EventTime eventTime,
    Exception audioSinkError
)

Called when AudioSink has encountered an error.

void

Called when an AudioTrack has been initialized.

void

Called when an AudioTrack has been released.

void
@UnstableApi
onAudioUnderrun(
    AnalyticsListener.EventTime eventTime,
    int bufferSize,
    long bufferSizeMs,
    long elapsedSinceLastFeedMs
)

Called when an audio underrun occurs.

void

Called when the player's available commands changed.

void

Called when there is a change in the CueGroup.

void

This method is deprecated.

Use onCues instead.

void

Called when the device information changes

void
@UnstableApi
onDeviceVolumeChanged(
    AnalyticsListener.EventTime eventTime,
    int volume,
    boolean muted
)

Called when the device volume or mute state changes.

void

Called each time drm keys are loaded.

void

Called each time offline drm keys are removed.

void

Called each time offline drm keys are restored.

void

This method is deprecated.

Implement onDrmSessionAcquired instead.

void

Called each time a drm session is acquired.

void

Called when a drm error occurs.

void

Called each time a drm session is released.

void
@UnstableApi
onDroppedVideoFrames(
    AnalyticsListener.EventTime eventTime,
    int droppedFrames,
    long elapsedMs
)

Called after video frames have been dropped.

void
@UnstableApi
onIsLoadingChanged(
    AnalyticsListener.EventTime eventTime,
    boolean isLoading
)

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

void
@UnstableApi
onIsPlayingChanged(
    AnalyticsListener.EventTime eventTime,
    boolean isPlaying
)

Called when the player starts or stops playing.

void
@UnstableApi
onLoadCanceled(
    AnalyticsListener.EventTime eventTime,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)

Called when a media source canceled loading data.

void
@UnstableApi
onLoadCompleted(
    AnalyticsListener.EventTime eventTime,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)

Called when a media source completed loading data.

void
@UnstableApi
onLoadStarted(
    AnalyticsListener.EventTime eventTime,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)

Called when a media source started loading data.

void
@UnstableApi
onLoadingChanged(
    AnalyticsListener.EventTime eventTime,
    boolean isLoading
)

This method is deprecated.

Use onIsLoadingChanged instead.

void
@UnstableApi
onMaxSeekToPreviousPositionChanged(
    AnalyticsListener.EventTime eventTime,
    long maxSeekToPreviousPositionMs
)

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

void

Called when playback transitions to a different media item.

void

Called when the combined MediaMetadata changes.

void

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

void
@UnstableApi
onPlayWhenReadyChanged(
    AnalyticsListener.EventTime eventTime,
    boolean playWhenReady,
    @Player.PlayWhenReadyChangeReason int reason
)

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

void

Called when the playback parameters changed.

void

Called when the playback state changed.

void

Called when playback suppression reason changed.

void

Called when the PlaybackException returned by getPlayerError changes.

void

Called when the Player is released.

void
@UnstableApi
onPlayerStateChanged(
    AnalyticsListener.EventTime eventTime,
    boolean playWhenReady,
    @Player.State int playbackState
)

This method is deprecated.

Use onPlaybackStateChanged and onPlayWhenReadyChanged instead.

void

Called when the playlist MediaMetadata changes.

void

This method is deprecated.

Use onPositionDiscontinuity instead.

void
@UnstableApi
onRenderedFirstFrame(
    AnalyticsListener.EventTime eventTime,
    Object output,
    long renderTimeMs
)

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.

void
@UnstableApi
onRendererReadyChanged(
    AnalyticsListener.EventTime eventTime,
    int rendererIndex,
    @C.TrackType int rendererTrackType,
    boolean isRendererReady
)

Called each time a renderer starts or stops allowing playback to be ready.

void

Called when the repeat mode changed.

void
@UnstableApi
onSeekBackIncrementChanged(
    AnalyticsListener.EventTime eventTime,
    long seekBackIncrementMs
)

Called when the seek back increment changed.

void
@UnstableApi
onSeekForwardIncrementChanged(
    AnalyticsListener.EventTime eventTime,
    long seekForwardIncrementMs
)

Called when the seek forward increment changed.

void

This method is deprecated.

Use onPositionDiscontinuity instead, listening to changes with DISCONTINUITY_REASON_SEEK.

void
@UnstableApi
onShuffleModeChanged(
    AnalyticsListener.EventTime eventTime,
    boolean shuffleModeEnabled
)

Called when the shuffle mode changed.

void
@UnstableApi
onSkipSilenceEnabledChanged(
    AnalyticsListener.EventTime eventTime,
    boolean skipSilenceEnabled
)

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

void
@UnstableApi
onSurfaceSizeChanged(
    AnalyticsListener.EventTime eventTime,
    int width,
    int height
)

Called when the output surface size changed.

void

Called when the timeline changed.

void

Called when track selection parameters change.

void

Called when the tracks change.

void

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

void
@UnstableApi
onVideoCodecError(
    AnalyticsListener.EventTime eventTime,
    Exception videoCodecError
)

Called when a video decoder encounters an error.

void
@UnstableApi
onVideoDecoderInitialized(
    AnalyticsListener.EventTime eventTime,
    String decoderName,
    long initializationDurationMs
)

This method is deprecated.

Use onVideoDecoderInitialized.

void
@UnstableApi
onVideoDecoderInitialized(
    AnalyticsListener.EventTime eventTime,
    String decoderName,
    long initializedTimestampMs,
    long initializationDurationMs
)

Called when a video renderer creates a decoder.

void
@UnstableApi
onVideoDecoderReleased(
    AnalyticsListener.EventTime eventTime,
    String decoderName
)

Called when a video renderer releases a decoder.

void
@UnstableApi
onVideoEnabled(
    AnalyticsListener.EventTime eventTime,
    DecoderCounters decoderCounters
)

Called when a video renderer is enabled.

void
@UnstableApi
onVideoFrameProcessingOffset(
    AnalyticsListener.EventTime eventTime,
    long totalProcessingOffsetUs,
    int frameCount
)

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

void
@UnstableApi
onVideoInputFormatChanged(
    AnalyticsListener.EventTime eventTime,
    Format format,
    @Nullable DecoderReuseEvaluation decoderReuseEvaluation
)

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

void
@UnstableApi
onVideoSizeChanged(
    AnalyticsListener.EventTime eventTime,
    int width,
    int height,
    int unappliedRotationDegrees,
    float pixelWidthHeightRatio
)

This method is deprecated.

Implement onVideoSizeChanged instead.

void

Called when the volume changes.

Public methods

create

public static @Nullable MediaMetricsListener create(Context context)

Creates a media metrics listener.

Parameters
Context context

A context.

Returns
@Nullable MediaMetricsListener

The MediaMetricsListener, or null if the media metrics service isn't available.

getLogSessionId

public LogSessionId getLogSessionId()

Returns the LogSessionId used by this listener.

onAdPlaybackStarted

public void onAdPlaybackStarted(
    AnalyticsListener.EventTime eventTime,
    String contentSessionId,
    String adSessionId
)

Called when a session is interrupted by ad playback.

Parameters
AnalyticsListener.EventTime eventTime

The EventTime at which the ad playback starts.

String contentSessionId

The session identifier of the content session.

String adSessionId

The identifier of the ad session.

onBandwidthEstimate

public void onBandwidthEstimate(
    AnalyticsListener.EventTime eventTime,
    int totalLoadTimeMs,
    long totalBytesLoaded,
    long bitrateEstimate
)

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

Parameters
AnalyticsListener.EventTime eventTime

The event time.

int totalLoadTimeMs

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

long totalBytesLoaded

The total bytes loaded this update is based on.

long bitrateEstimate

The bandwidth estimate, in bits per second.

onDownstreamFormatChanged

public void onDownstreamFormatChanged(
    AnalyticsListener.EventTime eventTime,
    MediaLoadData mediaLoadData
)

Called when the downstream format sent to the renderers changed.

Parameters
AnalyticsListener.EventTime eventTime

The event time.

MediaLoadData mediaLoadData

The MediaLoadData defining the newly selected media data.

onEvents

public void onEvents(Player player, AnalyticsListener.Events events)

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.

AnalyticsListener.Events events

The Events that occurred in this iteration.

onLoadError

public void onLoadError(
    AnalyticsListener.EventTime eventTime,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData,
    IOException error,
    boolean wasCanceled
)

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
AnalyticsListener.EventTime eventTime

The event time.

LoadEventInfo loadEventInfo

The LoadEventInfo defining the load event.

MediaLoadData mediaLoadData

The MediaLoadData defining the data being loaded.

IOException error

The load error.

boolean wasCanceled

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

onPlayerError

public void onPlayerError(
    AnalyticsListener.EventTime eventTime,
    PlaybackException error
)

Called when a fatal player error occurred.

Implementations of Player may pass an instance of a subclass of to this method in order to include more information about the error.

Parameters
AnalyticsListener.EventTime eventTime

The event time.

PlaybackException error

The error.

onPositionDiscontinuity

public void onPositionDiscontinuity(
    AnalyticsListener.EventTime eventTime,
    Player.PositionInfo oldPosition,
    Player.PositionInfo newPosition,
    @Player.DiscontinuityReason int reason
)

Called when a position discontinuity occurred.

Parameters
AnalyticsListener.EventTime eventTime

The event time.

Player.PositionInfo oldPosition

The position before the discontinuity.

Player.PositionInfo newPosition

The position after the discontinuity.

@Player.DiscontinuityReason int reason

The reason for the position discontinuity.

onSessionActive

public void onSessionActive(
    AnalyticsListener.EventTime eventTime,
    String sessionId
)

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

Parameters
AnalyticsListener.EventTime eventTime

The EventTime at which the session becomes active.

String sessionId

The identifier of the session.

onSessionCreated

public void onSessionCreated(
    AnalyticsListener.EventTime eventTime,
    String sessionId
)

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

Parameters
AnalyticsListener.EventTime eventTime

The EventTime at which the session is created.

String sessionId

The identifier of the new session.

onSessionFinished

public void onSessionFinished(
    AnalyticsListener.EventTime eventTime,
    String sessionId,
    boolean automaticTransitionToNextPlayback
)

Called when a session is permanently finished.

Parameters
AnalyticsListener.EventTime eventTime

The EventTime at which the session finished.

String sessionId

The identifier of the finished session.

boolean automaticTransitionToNextPlayback

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

onVideoDisabled

public void onVideoDisabled(
    AnalyticsListener.EventTime eventTime,
    DecoderCounters decoderCounters
)

Called when a video renderer is disabled.

Parameters
AnalyticsListener.EventTime eventTime

The event time.

DecoderCounters decoderCounters

DecoderCounters that were updated by the renderer.

onVideoSizeChanged

public void onVideoSizeChanged(
    AnalyticsListener.EventTime 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.

Parameters
AnalyticsListener.EventTime eventTime

The event time.

VideoSize videoSize

The new size of the video.