Search in sources :

Example 1 with RobolectricUtil

use of com.google.android.exoplayer2.robolectric.RobolectricUtil in project ExoPlayer by google.

the class TestPlayerRunHelper method runUntilSleepingForOffload.

/**
 * Runs tasks of the main {@link Looper} until {@link
 * ExoPlayer.AudioOffloadListener#onExperimentalSleepingForOffloadChanged(boolean)} is called or a
 * playback error occurs.
 *
 * <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
 *
 * @param player The {@link Player}.
 * @param expectedSleepForOffload The expected sleep of offload state.
 * @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
 *     exceeded.
 */
public static void runUntilSleepingForOffload(ExoPlayer player, boolean expectedSleepForOffload) throws TimeoutException {
    verifyMainTestThread(player);
    AtomicBoolean receiverCallback = new AtomicBoolean(false);
    ExoPlayer.AudioOffloadListener listener = new ExoPlayer.AudioOffloadListener() {

        @Override
        public void onExperimentalSleepingForOffloadChanged(boolean sleepingForOffload) {
            if (sleepingForOffload == expectedSleepForOffload) {
                receiverCallback.set(true);
            }
        }
    };
    player.addAudioOffloadListener(listener);
    runMainLooperUntil(() -> receiverCallback.get() || player.getPlayerError() != null);
    if (player.getPlayerError() != null) {
        throw new IllegalStateException(player.getPlayerError());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExoPlayer(com.google.android.exoplayer2.ExoPlayer)

Example 2 with RobolectricUtil

use of com.google.android.exoplayer2.robolectric.RobolectricUtil in project ExoPlayer by google.

the class TestPlayerRunHelper method runUntilRenderedFirstFrame.

/**
 * 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}..
 *
 * @param player The {@link Player}.
 * @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
 *     exceeded.
 */
public static void runUntilRenderedFirstFrame(ExoPlayer player) throws TimeoutException {
    verifyMainTestThread(player);
    AtomicBoolean receivedCallback = new AtomicBoolean(false);
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onRenderedFirstFrame() {
            receivedCallback.set(true);
        }
    };
    player.addListener(listener);
    runMainLooperUntil(() -> receivedCallback.get() || player.getPlayerError() != null);
    player.removeListener(listener);
    if (player.getPlayerError() != null) {
        throw new IllegalStateException(player.getPlayerError());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Player(com.google.android.exoplayer2.Player) ExoPlayer(com.google.android.exoplayer2.ExoPlayer)

Example 3 with RobolectricUtil

use of com.google.android.exoplayer2.robolectric.RobolectricUtil in project ExoPlayer by google.

the class TestPlayerRunHelper method playUntilPosition.

/**
 * Calls {@link Player#play()}, runs tasks of the main {@link Looper} until the {@code player}
 * reaches the specified position or a playback error occurs, and then pauses the {@code player}.
 *
 * <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
 *
 * @param player The {@link Player}.
 * @param mediaItemIndex The index of the media item.
 * @param positionMs The position within the media item, in milliseconds.
 * @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
 *     exceeded.
 */
public static void playUntilPosition(ExoPlayer player, int mediaItemIndex, long positionMs) throws TimeoutException {
    verifyMainTestThread(player);
    Looper applicationLooper = Util.getCurrentOrMainLooper();
    AtomicBoolean messageHandled = new AtomicBoolean(false);
    player.createMessage((messageType, payload) -> {
        // Block playback thread until pause command has been sent from test thread.
        ConditionVariable blockPlaybackThreadCondition = new ConditionVariable();
        player.getClock().createHandler(applicationLooper, /* callback= */
        null).post(() -> {
            player.pause();
            messageHandled.set(true);
            blockPlaybackThreadCondition.open();
        });
        try {
            player.getClock().onThreadBlocked();
            blockPlaybackThreadCondition.block();
        } catch (InterruptedException e) {
        // Ignore.
        }
    }).setPosition(mediaItemIndex, positionMs).send();
    player.play();
    runMainLooperUntil(() -> messageHandled.get() || player.getPlayerError() != null);
    if (player.getPlayerError() != null) {
        throw new IllegalStateException(player.getPlayerError());
    }
}
Also used : ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) Looper(android.os.Looper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 4 with RobolectricUtil

use of com.google.android.exoplayer2.robolectric.RobolectricUtil in project ExoPlayer by google.

the class TestPlayerRunHelper method runUntilPositionDiscontinuity.

/**
 * Runs tasks of the main {@link Looper} until {@link
 * Player.Listener#onPositionDiscontinuity(Player.PositionInfo, Player.PositionInfo, int)} is
 * called with the specified {@link Player.DiscontinuityReason} or a playback error occurs.
 *
 * <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
 *
 * @param player The {@link Player}.
 * @param expectedReason The expected {@link Player.DiscontinuityReason}.
 * @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
 *     exceeded.
 */
public static void runUntilPositionDiscontinuity(Player player, @Player.DiscontinuityReason int expectedReason) throws TimeoutException {
    verifyMainTestThread(player);
    AtomicBoolean receivedCallback = new AtomicBoolean(false);
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onPositionDiscontinuity(Player.PositionInfo oldPosition, Player.PositionInfo newPosition, int reason) {
            if (reason == expectedReason) {
                receivedCallback.set(true);
            }
        }
    };
    player.addListener(listener);
    runMainLooperUntil(() -> receivedCallback.get() || player.getPlayerError() != null);
    player.removeListener(listener);
    if (player.getPlayerError() != null) {
        throw new IllegalStateException(player.getPlayerError());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Player(com.google.android.exoplayer2.Player) ExoPlayer(com.google.android.exoplayer2.ExoPlayer)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)3 Player (com.google.android.exoplayer2.Player)2 Looper (android.os.Looper)1 ConditionVariable (com.google.android.exoplayer2.util.ConditionVariable)1