Search in sources :

Example 86 with Player

use of com.google.android.exoplayer2.Player in project ExoPlayer by google.

the class StyledPlayerControlView method updateProgress.

private void updateProgress() {
    if (!isVisible() || !isAttachedToWindow) {
        return;
    }
    @Nullable Player player = this.player;
    long position = 0;
    long bufferedPosition = 0;
    if (player != null) {
        position = currentWindowOffset + player.getContentPosition();
        bufferedPosition = currentWindowOffset + player.getContentBufferedPosition();
    }
    if (positionView != null && !scrubbing) {
        positionView.setText(Util.getStringForTime(formatBuilder, formatter, position));
    }
    if (timeBar != null) {
        timeBar.setPosition(position);
        timeBar.setBufferedPosition(bufferedPosition);
    }
    if (progressUpdateListener != null) {
        progressUpdateListener.onProgressUpdate(position, bufferedPosition);
    }
    // Cancel any pending updates and schedule a new one if necessary.
    removeCallbacks(updateProgressAction);
    int playbackState = player == null ? Player.STATE_IDLE : player.getPlaybackState();
    if (player != null && player.isPlaying()) {
        long mediaTimeDelayMs = timeBar != null ? timeBar.getPreferredUpdateDelay() : MAX_UPDATE_INTERVAL_MS;
        // Limit delay to the start of the next full second to ensure position display is smooth.
        long mediaTimeUntilNextFullSecondMs = 1000 - position % 1000;
        mediaTimeDelayMs = Math.min(mediaTimeDelayMs, mediaTimeUntilNextFullSecondMs);
        // Calculate the delay until the next update in real time, taking playback speed into account.
        float playbackSpeed = player.getPlaybackParameters().speed;
        long delayMs = playbackSpeed > 0 ? (long) (mediaTimeDelayMs / playbackSpeed) : MAX_UPDATE_INTERVAL_MS;
        // Constrain the delay to avoid too frequent / infrequent updates.
        delayMs = Util.constrainValue(delayMs, timeBarMinUpdateIntervalMs, MAX_UPDATE_INTERVAL_MS);
        postDelayed(updateProgressAction, delayMs);
    } else if (playbackState != Player.STATE_ENDED && playbackState != Player.STATE_IDLE) {
        postDelayed(updateProgressAction, MAX_UPDATE_INTERVAL_MS);
    }
}
Also used : Player(com.google.android.exoplayer2.Player) ForwardingPlayer(com.google.android.exoplayer2.ForwardingPlayer) Nullable(androidx.annotation.Nullable) SuppressLint(android.annotation.SuppressLint)

Example 87 with Player

use of com.google.android.exoplayer2.Player in project ExoPlayer by google.

the class StyledPlayerControlView method dispatchPlay.

private void dispatchPlay(Player player) {
    @State int state = player.getPlaybackState();
    if (state == Player.STATE_IDLE) {
        player.prepare();
    } else if (state == Player.STATE_ENDED) {
        seekTo(player, player.getCurrentMediaItemIndex(), C.TIME_UNSET);
    }
    player.play();
}
Also used : State(com.google.android.exoplayer2.Player.State) SuppressLint(android.annotation.SuppressLint)

Example 88 with Player

use of com.google.android.exoplayer2.Player in project ExoPlayer by google.

the class StyledPlayerControlView method initTrackSelectionAdapter.

private void initTrackSelectionAdapter() {
    textTrackSelectionAdapter.clear();
    audioTrackSelectionAdapter.clear();
    if (player == null || !player.isCommandAvailable(Player.COMMAND_GET_TRACK_INFOS) || !player.isCommandAvailable(Player.COMMAND_SET_TRACK_SELECTION_PARAMETERS)) {
        return;
    }
    TracksInfo tracksInfo = player.getCurrentTracksInfo();
    audioTrackSelectionAdapter.init(gatherSupportedTrackInfosOfType(tracksInfo, C.TRACK_TYPE_AUDIO));
    if (controlViewLayoutManager.getShowButton(subtitleButton)) {
        textTrackSelectionAdapter.init(gatherSupportedTrackInfosOfType(tracksInfo, C.TRACK_TYPE_TEXT));
    } else {
        textTrackSelectionAdapter.init(ImmutableList.of());
    }
}
Also used : TracksInfo(com.google.android.exoplayer2.TracksInfo)

Example 89 with Player

use of com.google.android.exoplayer2.Player in project ExoPlayer by google.

the class StyledPlayerControlView method updateRepeatModeButton.

private void updateRepeatModeButton() {
    if (!isVisible() || !isAttachedToWindow || repeatToggleButton == null) {
        return;
    }
    if (repeatToggleModes == RepeatModeUtil.REPEAT_TOGGLE_MODE_NONE) {
        updateButton(/* enabled= */
        false, repeatToggleButton);
        return;
    }
    @Nullable Player player = this.player;
    if (player == null) {
        updateButton(/* enabled= */
        false, repeatToggleButton);
        repeatToggleButton.setImageDrawable(repeatOffButtonDrawable);
        repeatToggleButton.setContentDescription(repeatOffButtonContentDescription);
        return;
    }
    updateButton(/* enabled= */
    true, repeatToggleButton);
    switch(player.getRepeatMode()) {
        case Player.REPEAT_MODE_OFF:
            repeatToggleButton.setImageDrawable(repeatOffButtonDrawable);
            repeatToggleButton.setContentDescription(repeatOffButtonContentDescription);
            break;
        case Player.REPEAT_MODE_ONE:
            repeatToggleButton.setImageDrawable(repeatOneButtonDrawable);
            repeatToggleButton.setContentDescription(repeatOneButtonContentDescription);
            break;
        case Player.REPEAT_MODE_ALL:
            repeatToggleButton.setImageDrawable(repeatAllButtonDrawable);
            repeatToggleButton.setContentDescription(repeatAllButtonContentDescription);
            break;
        default:
    }
}
Also used : Player(com.google.android.exoplayer2.Player) ForwardingPlayer(com.google.android.exoplayer2.ForwardingPlayer) Nullable(androidx.annotation.Nullable)

Example 90 with Player

use of com.google.android.exoplayer2.Player in project ExoPlayer by google.

the class PlayerControlView method updateNavigation.

private void updateNavigation() {
    if (!isVisible() || !isAttachedToWindow) {
        return;
    }
    @Nullable Player player = this.player;
    boolean enableSeeking = false;
    boolean enablePrevious = false;
    boolean enableRewind = false;
    boolean enableFastForward = false;
    boolean enableNext = false;
    if (player != null) {
        enableSeeking = player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM);
        enablePrevious = player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS);
        enableRewind = player.isCommandAvailable(COMMAND_SEEK_BACK);
        enableFastForward = player.isCommandAvailable(COMMAND_SEEK_FORWARD);
        enableNext = player.isCommandAvailable(COMMAND_SEEK_TO_NEXT);
    }
    updateButton(showPreviousButton, enablePrevious, previousButton);
    updateButton(showRewindButton, enableRewind, rewindButton);
    updateButton(showFastForwardButton, enableFastForward, fastForwardButton);
    updateButton(showNextButton, enableNext, nextButton);
    if (timeBar != null) {
        timeBar.setEnabled(enableSeeking);
    }
}
Also used : Player(com.google.android.exoplayer2.Player) Nullable(androidx.annotation.Nullable)

Aggregations

Test (org.junit.Test)250 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)185 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)174 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)127 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)107 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)91 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)89 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)89 Listener (com.google.android.exoplayer2.Player.Listener)85 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)72 MediaSource (com.google.android.exoplayer2.source.MediaSource)68 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)67 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)66 Nullable (androidx.annotation.Nullable)56 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)49 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)49 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)49 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)49 FakeAdaptiveMediaSource (com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource)49 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)47