Migrate to Player.Listener

PiperOrigin-RevId: 390124675
This commit is contained in:
olly 2021-08-11 15:09:43 +01:00 committed by Oliver Woodman
parent 4aa5898029
commit 7dffb2dc4d
6 changed files with 41 additions and 43 deletions

View File

@ -140,7 +140,7 @@ public interface Player {
*
* <p>The provided {@link MediaMetadata} is a combination of the {@link MediaItem#mediaMetadata}
* and the static and dynamic metadata from the {@link TrackSelection#getFormat(int) track
* selections' formats} and {@link MetadataOutput#onMetadata(Metadata)}.
* selections' formats} and {@link Listener#onMetadata(Metadata)}.
*
* <p>{@link #onEvents(Player, Events)} will also be called to report this event along with
* other events that happen in the same {@link Looper} message queue iteration.
@ -1924,7 +1924,7 @@ public interface Player {
*
* <p>This {@link MediaMetadata} is a combination of the {@link MediaItem#mediaMetadata} and the
* static and dynamic metadata from the {@link TrackSelection#getFormat(int) track selections'
* formats} and {@link MetadataOutput#onMetadata(Metadata)}.
* formats} and {@link Listener#onMetadata(Metadata)}.
*/
MediaMetadata getMediaMetadata();

View File

@ -22,7 +22,6 @@ import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.source.ClippingMediaSource;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.TextOutput;
import com.google.android.exoplayer2.util.ConditionVariable;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.common.collect.ImmutableList;
@ -56,34 +55,22 @@ public final class ClippedPlaybackTest {
.setClipEndPositionMs(1000)
.build();
AtomicReference<SimpleExoPlayer> player = new AtomicReference<>();
CapturingTextOutput textOutput = new CapturingTextOutput();
ConditionVariable playbackEnded = new ConditionVariable();
TextCapturingPlaybackListener textCapturer = new TextCapturingPlaybackListener();
getInstrumentation()
.runOnMainSync(
() -> {
player.set(new SimpleExoPlayer.Builder(getInstrumentation().getContext()).build());
player.get().addTextOutput(textOutput);
player
.get()
.addListener(
new Player.Listener() {
@Override
public void onPlaybackStateChanged(@Player.State int playbackState) {
if (playbackState == Player.STATE_ENDED) {
playbackEnded.open();
}
}
});
player.get().addListener(textCapturer);
player.get().setMediaItem(mediaItem);
player.get().prepare();
player.get().play();
});
playbackEnded.block();
textCapturer.block();
getInstrumentation().runOnMainSync(() -> player.get().release());
getInstrumentation().waitForIdleSync();
assertThat(Iterables.getOnlyElement(Iterables.concat(textOutput.cues)).text.toString())
assertThat(Iterables.getOnlyElement(Iterables.concat(textCapturer.cues)).text.toString())
.isEqualTo("This is the first subtitle.");
}
@ -110,42 +97,32 @@ public final class ClippedPlaybackTest {
.setClipEndPositionMs(4_000)
.build());
AtomicReference<SimpleExoPlayer> player = new AtomicReference<>();
CapturingTextOutput textOutput = new CapturingTextOutput();
ConditionVariable playbackEnded = new ConditionVariable();
TextCapturingPlaybackListener textCapturer = new TextCapturingPlaybackListener();
getInstrumentation()
.runOnMainSync(
() -> {
player.set(new SimpleExoPlayer.Builder(getInstrumentation().getContext()).build());
player.get().addTextOutput(textOutput);
player
.get()
.addListener(
new Player.Listener() {
@Override
public void onPlaybackStateChanged(@Player.State int playbackState) {
if (playbackState == Player.STATE_ENDED) {
playbackEnded.open();
}
}
});
player.get().addListener(textCapturer);
player.get().setMediaItems(mediaItems);
player.get().prepare();
player.get().play();
});
playbackEnded.block();
textCapturer.block();
getInstrumentation().runOnMainSync(() -> player.get().release());
getInstrumentation().waitForIdleSync();
assertThat(Iterables.getOnlyElement(Iterables.concat(textOutput.cues)).text.toString())
assertThat(Iterables.getOnlyElement(Iterables.concat(textCapturer.cues)).text.toString())
.isEqualTo("This is the first subtitle.");
}
private static class CapturingTextOutput implements TextOutput {
private static class TextCapturingPlaybackListener implements Player.Listener {
private final ConditionVariable playbackEnded;
private final List<List<Cue>> cues;
private CapturingTextOutput() {
private TextCapturingPlaybackListener() {
playbackEnded = new ConditionVariable();
cues = new ArrayList<>();
}
@ -153,5 +130,16 @@ public final class ClippedPlaybackTest {
public void onCues(List<Cue> cues) {
this.cues.add(cues);
}
@Override
public void onPlaybackStateChanged(@Player.State int playbackState) {
if (playbackState == Player.STATE_ENDED) {
playbackEnded.open();
}
}
public void block() throws InterruptedException {
playbackEnded.block();
}
}
}

View File

@ -1490,7 +1490,7 @@ public class PlayerView extends FrameLayout implements AdViewProvider {
@Override
public void onCues(List<Cue> cues) {
if (subtitleView != null) {
subtitleView.onCues(cues);
subtitleView.setCues(cues);
}
}

View File

@ -1531,7 +1531,7 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider {
@Override
public void onCues(List<Cue> cues) {
if (subtitleView != null) {
subtitleView.onCues(cues);
subtitleView.setCues(cues);
}
}

View File

@ -17,6 +17,7 @@ package com.google.android.exoplayer2.robolectric;
import android.graphics.Bitmap;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.dvbsi.AppInfoTable;
@ -63,9 +64,19 @@ public final class PlaybackOutput implements Dumper.Dumpable {
// TODO: Consider passing playback position into MetadataOutput and TextOutput. Calling
// player.getCurrentPosition() inside onMetadata/Cues will likely be non-deterministic
// because renderer-thread != playback-thread.
player.addMetadataOutput(metadatas::add);
player.addListener(
new Player.Listener() {
@Override
public void onMetadata(Metadata metadata) {
metadatas.add(metadata);
}
@Override
public void onCues(List<Cue> cues) {
// TODO(internal b/174661563): Output subtitle data when it's not flaky.
}
});
}
/**
* Create an instance that captures the metadata and text output from {@code player} and the audio

View File

@ -27,7 +27,6 @@ import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.util.ConditionVariable;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.VideoListener;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@ -252,7 +251,7 @@ public class TestPlayerRunHelper {
}
/**
* Runs tasks of the main {@link Looper} until the {@link VideoListener#onRenderedFirstFrame}
* Runs tasks of the main {@link Looper} until the {@link Player.Listener#onRenderedFirstFrame}
* callback is called or a playback error occurs.
*
* <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}..