mirror of
https://github.com/samsonjs/media.git
synced 2026-04-09 11:55:46 +00:00
Add Player.getCurrentMediaItem()
PiperOrigin-RevId: 316650017
This commit is contained in:
parent
5b28cb5209
commit
b7233c28e9
4 changed files with 93 additions and 5 deletions
|
|
@ -74,7 +74,9 @@
|
|||
* `SimpleDecoderVideoRenderer` and `SimpleDecoderAudioRenderer` renamed to
|
||||
`DecoderVideoRenderer` and `DecoderAudioRenderer` respectively, and
|
||||
generalized to work with `Decoder` rather than `SimpleDecoder`.
|
||||
* Add media item based playlist API to Player.
|
||||
* Add media item based playlist API to `Player`.
|
||||
* Add `getCurrentMediaItem` to `Player`.
|
||||
* Remove deprecated members in `DefaultTrackSelector`.
|
||||
* Add `Player.DeviceComponent` and implement it for `SimpleExoPlayer` so
|
||||
that the device volume can be controlled by player.
|
||||
* Parse track titles from Matroska files
|
||||
|
|
|
|||
|
|
@ -158,11 +158,31 @@ public abstract class BasePlayer implements Player {
|
|||
getCurrentWindowIndex(), getRepeatModeForNavigation(), getShuffleModeEnabled());
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #getCurrentMediaItem()} and {@link MediaItem.PlaybackProperties#tag}
|
||||
* instead.
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
@Nullable
|
||||
public final Object getCurrentTag() {
|
||||
Timeline timeline = getCurrentTimeline();
|
||||
return timeline.isEmpty() ? null : timeline.getWindow(getCurrentWindowIndex(), window).tag;
|
||||
if (timeline.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
@Nullable
|
||||
MediaItem.PlaybackProperties playbackProperties =
|
||||
timeline.getWindow(getCurrentWindowIndex(), window).mediaItem.playbackProperties;
|
||||
return playbackProperties != null ? playbackProperties.tag : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public final MediaItem getCurrentMediaItem() {
|
||||
Timeline timeline = getCurrentTimeline();
|
||||
return timeline.isEmpty()
|
||||
? null
|
||||
: timeline.getWindow(getCurrentWindowIndex(), window).mediaItem;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1242,10 +1242,19 @@ public interface Player {
|
|||
int getPreviousWindowIndex();
|
||||
|
||||
/**
|
||||
* Returns the tag of the currently playing window in the timeline. May be null if no tag is set
|
||||
* or the timeline is not yet available.
|
||||
* @deprecated Use {@link #getCurrentMediaItem()} and {@link MediaItem.PlaybackProperties#tag}
|
||||
* instead.
|
||||
*/
|
||||
@Nullable Object getCurrentTag();
|
||||
@Deprecated
|
||||
@Nullable
|
||||
Object getCurrentTag();
|
||||
|
||||
/**
|
||||
* Returns the media item of the current window in the timeline. May be null if the timeline is
|
||||
* empty.
|
||||
*/
|
||||
@Nullable
|
||||
MediaItem getCurrentMediaItem();
|
||||
|
||||
/**
|
||||
* Returns the duration of the current content window or ad in milliseconds, or {@link
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ import com.google.android.exoplayer2.source.MediaSource;
|
|||
import com.google.android.exoplayer2.source.MediaSource.MediaPeriodId;
|
||||
import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispatcher;
|
||||
import com.google.android.exoplayer2.source.SampleStream;
|
||||
import com.google.android.exoplayer2.source.SilenceMediaSource;
|
||||
import com.google.android.exoplayer2.source.TrackGroup;
|
||||
import com.google.android.exoplayer2.source.TrackGroupArray;
|
||||
import com.google.android.exoplayer2.source.ads.AdPlaybackState;
|
||||
|
|
@ -6709,6 +6710,62 @@ public final class ExoPlayerTest {
|
|||
.isEqualTo(rendererStreamOffsetsUs.get(0) + periodDurationUs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mediaItemOfSources_correctInTimelineWindows() throws Exception {
|
||||
SilenceMediaSource.Factory factory =
|
||||
new SilenceMediaSource.Factory().setDurationUs(C.msToUs(100_000));
|
||||
final Player[] playerHolder = {null};
|
||||
ActionSchedule actionSchedule =
|
||||
new ActionSchedule.Builder(TAG)
|
||||
.executeRunnable(
|
||||
new PlayerRunnable() {
|
||||
@Override
|
||||
public void run(SimpleExoPlayer player) {
|
||||
playerHolder[0] = player;
|
||||
}
|
||||
})
|
||||
.waitForPlaybackState(Player.STATE_READY)
|
||||
.seek(/* positionMs= */ 0)
|
||||
.waitForPlaybackState(Player.STATE_ENDED)
|
||||
.build();
|
||||
List<MediaItem> currentMediaItems = new ArrayList<>();
|
||||
List<MediaItem> initialMediaItems = new ArrayList<>();
|
||||
Player.EventListener eventListener =
|
||||
new Player.EventListener() {
|
||||
@Override
|
||||
public void onTimelineChanged(Timeline timeline, int reason) {
|
||||
if (reason != Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED) {
|
||||
return;
|
||||
}
|
||||
Window window = new Window();
|
||||
for (int i = 0; i < timeline.getWindowCount(); i++) {
|
||||
initialMediaItems.add(timeline.getWindow(i, window).mediaItem);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPositionDiscontinuity(int reason) {
|
||||
currentMediaItems.add(playerHolder[0].getCurrentMediaItem());
|
||||
}
|
||||
};
|
||||
new ExoPlayerTestRunner.Builder(context)
|
||||
.setEventListener(eventListener)
|
||||
.setActionSchedule(actionSchedule)
|
||||
.setMediaSources(
|
||||
factory.setTag("1").createMediaSource(),
|
||||
factory.setTag("2").createMediaSource(),
|
||||
factory.setTag("3").createMediaSource())
|
||||
.build()
|
||||
.start()
|
||||
.blockUntilActionScheduleFinished(TIMEOUT_MS)
|
||||
.blockUntilEnded(TIMEOUT_MS);
|
||||
|
||||
assertThat(currentMediaItems.get(0).playbackProperties.tag).isEqualTo("1");
|
||||
assertThat(currentMediaItems.get(1).playbackProperties.tag).isEqualTo("2");
|
||||
assertThat(currentMediaItems.get(2).playbackProperties.tag).isEqualTo("3");
|
||||
assertThat(initialMediaItems).containsExactlyElementsIn(currentMediaItems);
|
||||
}
|
||||
|
||||
// Internal methods.
|
||||
|
||||
private static ActionSchedule.Builder addSurfaceSwitch(ActionSchedule.Builder builder) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue