Skip to content

Commit 5b18c2d

Browse files
toniheichristosts
authored andcommitted
Extend command GET_CURRENT_MEDIA_ITEM to more methods.
We currently only document it for the getCurrentMediaItem(), but the command was always meant to cover all information about the current media item and the position therein. To correctly hide information for controllers, we need to filter the Timeline when bundling the PlayerInfo class if only this command is available. PiperOrigin-RevId: 503098124 (cherry picked from commit f15b752)
1 parent b8b6ddf commit 5b18c2d

File tree

10 files changed

+564
-62
lines changed

10 files changed

+564
-62
lines changed

libraries/common/src/main/java/androidx/media3/common/Player.java

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,10 +1625,28 @@ default void onMetadata(Metadata metadata) {}
16251625
int COMMAND_SET_REPEAT_MODE = 15;
16261626

16271627
/**
1628-
* Command to get the currently playing {@link MediaItem}.
1628+
* Command to get information about the currently playing {@link MediaItem}.
16291629
*
1630-
*

The {@link #getCurrentMediaItem()} method must only be called if this command is {@linkplain

1631-
* #isCommandAvailable(int) available}.
1630+
*

The following methods must only be called if this command is {@linkplain

1631+
* #isCommandAvailable(int) available}:
1632+
*
1633+
*
    1634+
    *
  • {@link #getCurrentMediaItem()}
  • 1635+
    *
  • {@link #isCurrentMediaItemDynamic()}
  • 1636+
    *
  • {@link #isCurrentMediaItemLive()}
  • 1637+
    *
  • {@link #isCurrentMediaItemSeekable()}
  • 1638+
    *
  • {@link #getCurrentLiveOffset()}
  • 1639+
    *
  • {@link #getDuration()}
  • 1640+
    *
  • {@link #getCurrentPosition()}
  • 1641+
    *
  • {@link #getBufferedPosition()}
  • 1642+
    *
  • {@link #getContentDuration()}
  • 1643+
    *
  • {@link #getContentPosition()}
  • 1644+
    *
  • {@link #getContentBufferedPosition()}
  • 1645+
    *
  • {@link #getTotalBufferedDuration()}
  • 1646+
    *
  • {@link #isPlayingAd()}
  • 1647+
    *
  • {@link #getCurrentAdGroupIndex()}
  • 1648+
    *
  • {@link #getCurrentAdIndexInAdGroup()}
  • 1649+
    *
    16321650
    */
    16331651
    int COMMAND_GET_CURRENT_MEDIA_ITEM = 16;
    16341652

    @@ -1648,8 +1666,6 @@ default void onMetadata(Metadata metadata) {}
    16481666
    *
  • {@link #getPreviousMediaItemIndex()}
  • 16491667
    *
  • {@link #hasPreviousMediaItem()}
  • 16501668
    *
  • {@link #hasNextMediaItem()}
  • 1651-
    *
  • {@link #getCurrentAdGroupIndex()}
  • 1652-
    *
  • {@link #getCurrentAdIndexInAdGroup()}
  • 16531669
    *
    16541670
    */
    16551671
    int COMMAND_GET_TIMELINE = 17;
    @@ -2692,18 +2708,27 @@ default void onMetadata(Metadata metadata) {}
    26922708
    /**
    26932709
    * Returns the duration of the current content or ad in milliseconds, or {@link C#TIME_UNSET} if
    26942710
    * the duration is not known.
    2711+
    *
    2712+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2713+
    * #getAvailableCommands() available}.
    26952714
    */
    26962715
    long getDuration();
    26972716

    26982717
    /**
    26992718
    * Returns the playback position in the current content or ad, in milliseconds, or the prospective
    27002719
    * position in milliseconds if the {@link #getCurrentTimeline() current timeline} is empty.
    2720+
    *
    2721+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2722+
    * #getAvailableCommands() available}.
    27012723
    */
    27022724
    long getCurrentPosition();
    27032725

    27042726
    /**
    27052727
    * Returns an estimate of the position in the current content or ad up to which data is buffered,
    27062728
    * in milliseconds.
    2729+
    *
    2730+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2731+
    * #getAvailableCommands() available}.
    27072732
    */
    27082733
    long getBufferedPosition();
    27092734

    @@ -2717,6 +2742,9 @@ default void onMetadata(Metadata metadata) {}
    27172742
    /**
    27182743
    * Returns an estimate of the total buffered duration from the current position, in milliseconds.
    27192744
    * This includes pre-buffered data for subsequent ads and {@linkplain MediaItem media items}.
    2745+
    *
    2746+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2747+
    * #getAvailableCommands() available}.
    27202748
    */
    27212749
    long getTotalBufferedDuration();
    27222750

    @@ -2731,6 +2759,9 @@ default void onMetadata(Metadata metadata) {}
    27312759
    * Returns whether the current {@link MediaItem} is dynamic (may change when the {@link Timeline}
    27322760
    * is updated), or {@code false} if the {@link Timeline} is empty.
    27332761
    *
    2762+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2763+
    * #getAvailableCommands() available}.
    2764+
    *
    27342765
    * @see Timeline.Window#isDynamic
    27352766
    */
    27362767
    boolean isCurrentMediaItemDynamic();
    @@ -2746,6 +2777,9 @@ default void onMetadata(Metadata metadata) {}
    27462777
    * Returns whether the current {@link MediaItem} is live, or {@code false} if the {@link Timeline}
    27472778
    * is empty.
    27482779
    *
    2780+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2781+
    * #getAvailableCommands() available}.
    2782+
    *
    27492783
    * @see Timeline.Window#isLive()
    27502784
    */
    27512785
    boolean isCurrentMediaItemLive();
    @@ -2760,6 +2794,9 @@ default void onMetadata(Metadata metadata) {}
    27602794
    *
    27612795
    *

    Note that this offset may rely on an accurate local time, so this method may return an

    27622796
    * incorrect value if the difference between system clock and server clock is unknown.
    2797+
    *
    2798+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2799+
    * #getAvailableCommands() available}.
    27632800
    */
    27642801
    long getCurrentLiveOffset();
    27652802

    @@ -2774,18 +2811,26 @@ default void onMetadata(Metadata metadata) {}
    27742811
    * Returns whether the current {@link MediaItem} is seekable, or {@code false} if the {@link
    27752812
    * Timeline} is empty.
    27762813
    *
    2814+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2815+
    * #getAvailableCommands() available}.
    2816+
    *
    27772817
    * @see Timeline.Window#isSeekable
    27782818
    */
    27792819
    boolean isCurrentMediaItemSeekable();
    27802820

    2781-
    /** Returns whether the player is currently playing an ad. */
    2821+
    /**
    2822+
    * Returns whether the player is currently playing an ad.
    2823+
    *
    2824+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2825+
    * #getAvailableCommands() available}.
    2826+
    */
    27822827
    boolean isPlayingAd();
    27832828

    27842829
    /**
    27852830
    * If {@link #isPlayingAd()} returns true, returns the index of the ad group in the period
    27862831
    * currently being played. Returns {@link C#INDEX_UNSET} otherwise.
    27872832
    *
    2788-
    *

    This method must only be called if {@link #COMMAND_GET_TIMELINE} is {@linkplain

    2833+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    27892834
    * #getAvailableCommands() available}.
    27902835
    */
    27912836
    int getCurrentAdGroupIndex();
    @@ -2794,7 +2839,7 @@ default void onMetadata(Metadata metadata) {}
    27942839
    * If {@link #isPlayingAd()} returns true, returns the index of the ad in its ad group. Returns
    27952840
    * {@link C#INDEX_UNSET} otherwise.
    27962841
    *
    2797-
    *

    This method must only be called if {@link #COMMAND_GET_TIMELINE} is {@linkplain

    2842+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    27982843
    * #getAvailableCommands() available}.
    27992844
    */
    28002845
    int getCurrentAdIndexInAdGroup();
    @@ -2803,20 +2848,29 @@ default void onMetadata(Metadata metadata) {}
    28032848
    * If {@link #isPlayingAd()} returns {@code true}, returns the duration of the current content in
    28042849
    * milliseconds, or {@link C#TIME_UNSET} if the duration is not known. If there is no ad playing,
    28052850
    * the returned duration is the same as that returned by {@link #getDuration()}.
    2851+
    *
    2852+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2853+
    * #getAvailableCommands() available}.
    28062854
    */
    28072855
    long getContentDuration();
    28082856

    28092857
    /**
    28102858
    * If {@link #isPlayingAd()} returns {@code true}, returns the content position that will be
    28112859
    * played once all ads in the ad group have finished playing, in milliseconds. If there is no ad
    28122860
    * playing, the returned position is the same as that returned by {@link #getCurrentPosition()}.
    2861+
    *
    2862+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2863+
    * #getAvailableCommands() available}.
    28132864
    */
    28142865
    long getContentPosition();
    28152866

    28162867
    /**
    28172868
    * If {@link #isPlayingAd()} returns {@code true}, returns an estimate of the content position in
    28182869
    * the current content up to which data is buffered, in milliseconds. If there is no ad playing,
    28192870
    * the returned position is the same as that returned by {@link #getBufferedPosition()}.
    2871+
    *
    2872+
    *

    This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain

    2873+
    * #getAvailableCommands() available}.
    28202874
    */
    28212875
    long getContentBufferedPosition();
    28222876

    libraries/common/src/main/java/androidx/media3/common/Timeline.java

    Lines changed: 33 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1416,6 +1416,39 @@ public final Bundle toBundle() {
    14161416
    return bundle;
    14171417
    }
    14181418

    1419+
    /**
    1420+
    * Returns a {@link Bundle} containing just the specified {@link Window}.
    1421+
    *
    1422+
    *

    The {@link #getWindow(int, Window)} windows} and {@link #getPeriod(int, Period) periods} of

    1423+
    * an instance restored by {@link #CREATOR} may have missing fields as described in {@link
    1424+
    * Window#toBundle()} and {@link Period#toBundle()}.
    1425+
    *
    1426+
    * @param windowIndex The index of the {@link Window} to include in the {@link Bundle}.
    1427+
    */
    1428+
    @UnstableApi
    1429+
    public final Bundle toBundleWithOneWindowOnly(int windowIndex) {
    1430+
    Window window = getWindow(windowIndex, new Window(), /* defaultPositionProjectionUs= */ 0);
    1431+
    1432+
    List<Bundle> periodBundles = new ArrayList<>();
    1433+
    Period period = new Period();
    1434+
    for (int i = window.firstPeriodIndex; i <= window.lastPeriodIndex; i++) {
    1435+
    getPeriod(i, period, /* setIds= */ false);
    1436+
    period.windowIndex = 0;
    1437+
    periodBundles.add(period.toBundle());
    1438+
    }
    1439+
    1440+
    window.lastPeriodIndex = window.lastPeriodIndex - window.firstPeriodIndex;
    1441+
    window.firstPeriodIndex = 0;
    1442+
    Bundle windowBundle = window.toBundle();
    1443+
    1444+
    Bundle bundle = new Bundle();
    1445+
    BundleUtil.putBinder(
    1446+
    bundle, FIELD_WINDOWS, new BundleListRetriever(ImmutableList.of(windowBundle)));
    1447+
    BundleUtil.putBinder(bundle, FIELD_PERIODS, new BundleListRetriever(periodBundles));
    1448+
    bundle.putIntArray(FIELD_SHUFFLED_WINDOW_INDICES, new int[] {0});
    1449+
    return bundle;
    1450+
    }
    1451+
    14191452
    /**
    14201453
    * Object that can restore a {@link Timeline} from a {@link Bundle}.
    14211454
    *

    libraries/session/src/main/java/androidx/media3/session/MediaSessionImpl.java

    Lines changed: 0 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -810,8 +810,6 @@ public void onMediaItemTransition(
    810810
    if (player == null) {
    811811
    return;
    812812
    }
    813-
    // Note: OK to omit mediaItem here, because PlayerInfo changed message will copy playerInfo
    814-
    // with sessionPositionInfo, which includes current window index.
    815813
    session.playerInfo = session.playerInfo.copyWithMediaItemTransitionReason(reason);
    816814
    session.onPlayerInfoChangedHandler.sendPlayerInfoChangedMessage(
    817815
    /* excludeTimeline= */ true, /* excludeTracks= */ true);

    0 commit comments

    Comments
     (0)