public interface

PlaybackSessionManager

 androidx.media3.exoplayer.analytics.PlaybackSessionManager

Subclasses:

DefaultPlaybackSessionManager

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-exoplayer', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-exoplayer
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-exoplayer:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Manager for active playback sessions.

The manager keeps track of the association between window index and/or media period id to session identifier.

Summary

Methods
public booleanbelongsToSession(AnalyticsListener.EventTime eventTime, java.lang.String sessionId)

Returns whether an event time belong to a session.

public voidfinishAllSessions(AnalyticsListener.EventTime eventTime)

Finishes all existing sessions and calls their respective PlaybackSessionManager.Listener callback.

public java.lang.StringgetActiveSessionId()

Returns the session identifier of the session that is currently actively playing, or null if there no such session.

public java.lang.StringgetSessionForMediaPeriodId(Timeline timeline, MediaSource.MediaPeriodId mediaPeriodId)

Returns the session identifier for the given media period id.

public voidsetListener(PlaybackSessionManager.Listener listener)

Sets the listener to be notified of session updates.

public voidupdateSessions(AnalyticsListener.EventTime eventTime)

Updates or creates sessions based on a player AnalyticsListener.EventTime.

public voidupdateSessionsWithDiscontinuity(AnalyticsListener.EventTime eventTime, int reason)

Updates or creates sessions based on a position discontinuity at AnalyticsListener.EventTime.

public voidupdateSessionsWithTimelineChange(AnalyticsListener.EventTime eventTime)

Updates or creates sessions based on a Timeline change at AnalyticsListener.EventTime.

Methods

public void setListener(PlaybackSessionManager.Listener listener)

Sets the listener to be notified of session updates. Must be called before the session manager is used.

Parameters:

listener: The PlaybackSessionManager.Listener to be notified of session updates.

public java.lang.String getSessionForMediaPeriodId(Timeline timeline, MediaSource.MediaPeriodId mediaPeriodId)

Returns the session identifier for the given media period id.

Note that this will reserve a new session identifier if it doesn't exist yet, but will not call any PlaybackSessionManager.Listener callbacks.

Parameters:

timeline: The timeline, mediaPeriodId is part of.
mediaPeriodId: A MediaSource.MediaPeriodId.

public boolean belongsToSession(AnalyticsListener.EventTime eventTime, java.lang.String sessionId)

Returns whether an event time belong to a session.

Parameters:

eventTime: The AnalyticsListener.EventTime.
sessionId: A session identifier.

Returns:

Whether the event belongs to the specified session.

public void updateSessions(AnalyticsListener.EventTime eventTime)

Updates or creates sessions based on a player AnalyticsListener.EventTime.

Call PlaybackSessionManager or PlaybackSessionManager if the event is a Timeline change or a position discontinuity respectively.

Parameters:

eventTime: The AnalyticsListener.EventTime.

public void updateSessionsWithTimelineChange(AnalyticsListener.EventTime eventTime)

Updates or creates sessions based on a Timeline change at AnalyticsListener.EventTime.

Should be called instead of PlaybackSessionManager if a Timeline change occurred.

Parameters:

eventTime: The AnalyticsListener.EventTime with the timeline change.

public void updateSessionsWithDiscontinuity(AnalyticsListener.EventTime eventTime, int reason)

Updates or creates sessions based on a position discontinuity at AnalyticsListener.EventTime.

Should be called instead of PlaybackSessionManager if a position discontinuity occurred.

Parameters:

eventTime: The AnalyticsListener.EventTime of the position discontinuity.
reason: The Player.DiscontinuityReason.

public java.lang.String getActiveSessionId()

Returns the session identifier of the session that is currently actively playing, or null if there no such session.

public void finishAllSessions(AnalyticsListener.EventTime eventTime)

Finishes all existing sessions and calls their respective PlaybackSessionManager.Listener callback.

Parameters:

eventTime: The event time at which sessions are finished.

Source

/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.exoplayer.analytics;

import androidx.annotation.Nullable;
import androidx.media3.common.Player.DiscontinuityReason;
import androidx.media3.common.Timeline;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.analytics.AnalyticsListener.EventTime;
import androidx.media3.exoplayer.source.MediaSource.MediaPeriodId;

/**
 * Manager for active playback sessions.
 *
 * <p>The manager keeps track of the association between window index and/or media period id to
 * session identifier.
 */
@UnstableApi
public interface PlaybackSessionManager {

  /** A listener for session updates. */
  interface Listener {

    /**
     * Called when a new session is created as a result of {@link #updateSessions(EventTime)}.
     *
     * @param eventTime The {@link EventTime} at which the session is created.
     * @param sessionId The identifier of the new session.
     */
    void onSessionCreated(EventTime eventTime, String sessionId);

    /**
     * Called when a session becomes active, i.e. playing in the foreground.
     *
     * @param eventTime The {@link EventTime} at which the session becomes active.
     * @param sessionId The identifier of the session.
     */
    void onSessionActive(EventTime eventTime, String sessionId);

    /**
     * Called when a session is interrupted by ad playback.
     *
     * @param eventTime The {@link EventTime} at which the ad playback starts.
     * @param contentSessionId The session identifier of the content session.
     * @param adSessionId The identifier of the ad session.
     */
    void onAdPlaybackStarted(EventTime eventTime, String contentSessionId, String adSessionId);

    /**
     * Called when a session is permanently finished.
     *
     * @param eventTime The {@link EventTime} at which the session finished.
     * @param sessionId The identifier of the finished session.
     * @param automaticTransitionToNextPlayback Whether the session finished because of an automatic
     *     transition to the next playback item.
     */
    void onSessionFinished(
        EventTime eventTime, String sessionId, boolean automaticTransitionToNextPlayback);
  }

  /**
   * Sets the listener to be notified of session updates. Must be called before the session manager
   * is used.
   *
   * @param listener The {@link Listener} to be notified of session updates.
   */
  void setListener(Listener listener);

  /**
   * Returns the session identifier for the given media period id.
   *
   * <p>Note that this will reserve a new session identifier if it doesn't exist yet, but will not
   * call any {@link Listener} callbacks.
   *
   * @param timeline The timeline, {@code mediaPeriodId} is part of.
   * @param mediaPeriodId A {@link MediaPeriodId}.
   */
  String getSessionForMediaPeriodId(Timeline timeline, MediaPeriodId mediaPeriodId);

  /**
   * Returns whether an event time belong to a session.
   *
   * @param eventTime The {@link EventTime}.
   * @param sessionId A session identifier.
   * @return Whether the event belongs to the specified session.
   */
  boolean belongsToSession(EventTime eventTime, String sessionId);

  /**
   * Updates or creates sessions based on a player {@link EventTime}.
   *
   * <p>Call {@link #updateSessionsWithTimelineChange(EventTime)} or {@link
   * #updateSessionsWithDiscontinuity(EventTime, int)} if the event is a {@link Timeline} change or
   * a position discontinuity respectively.
   *
   * @param eventTime The {@link EventTime}.
   */
  void updateSessions(EventTime eventTime);

  /**
   * Updates or creates sessions based on a {@link Timeline} change at {@link EventTime}.
   *
   * <p>Should be called instead of {@link #updateSessions(EventTime)} if a {@link Timeline} change
   * occurred.
   *
   * @param eventTime The {@link EventTime} with the timeline change.
   */
  void updateSessionsWithTimelineChange(EventTime eventTime);

  /**
   * Updates or creates sessions based on a position discontinuity at {@link EventTime}.
   *
   * <p>Should be called instead of {@link #updateSessions(EventTime)} if a position discontinuity
   * occurred.
   *
   * @param eventTime The {@link EventTime} of the position discontinuity.
   * @param reason The {@link DiscontinuityReason}.
   */
  void updateSessionsWithDiscontinuity(EventTime eventTime, @DiscontinuityReason int reason);

  /**
   * Returns the session identifier of the session that is currently actively playing, or {@code
   * null} if there no such session.
   */
  @Nullable
  String getActiveSessionId();

  /**
   * Finishes all existing sessions and calls their respective {@link
   * Listener#onSessionFinished(EventTime, String, boolean)} callback.
   *
   * @param eventTime The event time at which sessions are finished.
   */
  void finishAllSessions(EventTime eventTime);
}