Search in sources :

Example 1 with TimeBar

use of com.google.android.exoplayer2.ui.TimeBar in project bolan by qiusunshine.

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) SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) ForwardingPlayer(com.google.android.exoplayer2.ForwardingPlayer) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) Nullable(androidx.annotation.Nullable) SuppressLint(android.annotation.SuppressLint)

Example 2 with TimeBar

use of com.google.android.exoplayer2.ui.TimeBar in project bolan by qiusunshine.

the class StyledPlayerControlView method updateTimeline.

private void updateTimeline() {
    @Nullable Player player = this.player;
    if (player == null) {
        return;
    }
    multiWindowTimeBar = showMultiWindowTimeBar && canShowMultiWindowTimeBar(player.getCurrentTimeline(), window);
    currentWindowOffset = 0;
    long durationUs = 0;
    int adGroupCount = 0;
    Timeline timeline = player.getCurrentTimeline();
    if (!timeline.isEmpty()) {
        int currentWindowIndex = player.getCurrentWindowIndex();
        int firstWindowIndex = multiWindowTimeBar ? 0 : currentWindowIndex;
        int lastWindowIndex = multiWindowTimeBar ? timeline.getWindowCount() - 1 : currentWindowIndex;
        for (int i = firstWindowIndex; i <= lastWindowIndex; i++) {
            if (i == currentWindowIndex) {
                currentWindowOffset = C.usToMs(durationUs);
            }
            timeline.getWindow(i, window);
            if (window.durationUs == C.TIME_UNSET) {
                Assertions.checkState(!multiWindowTimeBar);
                break;
            }
            for (int j = window.firstPeriodIndex; j <= window.lastPeriodIndex; j++) {
                timeline.getPeriod(j, period);
                int removedGroups = period.getRemovedAdGroupCount();
                int totalGroups = period.getAdGroupCount();
                for (int adGroupIndex = removedGroups; adGroupIndex < totalGroups; adGroupIndex++) {
                    long adGroupTimeInPeriodUs = period.getAdGroupTimeUs(adGroupIndex);
                    if (adGroupTimeInPeriodUs == C.TIME_END_OF_SOURCE) {
                        if (period.durationUs == C.TIME_UNSET) {
                            // Don't show ad markers for postrolls in periods with unknown duration.
                            continue;
                        }
                        adGroupTimeInPeriodUs = period.durationUs;
                    }
                    long adGroupTimeInWindowUs = adGroupTimeInPeriodUs + period.getPositionInWindowUs();
                    if (adGroupTimeInWindowUs >= 0) {
                        if (adGroupCount == adGroupTimesMs.length) {
                            int newLength = adGroupTimesMs.length == 0 ? 1 : adGroupTimesMs.length * 2;
                            adGroupTimesMs = Arrays.copyOf(adGroupTimesMs, newLength);
                            playedAdGroups = Arrays.copyOf(playedAdGroups, newLength);
                        }
                        adGroupTimesMs[adGroupCount] = C.usToMs(durationUs + adGroupTimeInWindowUs);
                        playedAdGroups[adGroupCount] = period.hasPlayedAdGroup(adGroupIndex);
                        adGroupCount++;
                    }
                }
            }
            durationUs += window.durationUs;
        }
    }
    long durationMs = C.usToMs(durationUs);
    if (durationView != null) {
        durationView.setText(Util.getStringForTime(formatBuilder, formatter, durationMs));
    }
    if (timeBar != null) {
        timeBar.setDuration(durationMs);
        int extraAdGroupCount = extraAdGroupTimesMs.length;
        int totalAdGroupCount = adGroupCount + extraAdGroupCount;
        if (totalAdGroupCount > adGroupTimesMs.length) {
            adGroupTimesMs = Arrays.copyOf(adGroupTimesMs, totalAdGroupCount);
            playedAdGroups = Arrays.copyOf(playedAdGroups, totalAdGroupCount);
        }
        System.arraycopy(extraAdGroupTimesMs, 0, adGroupTimesMs, adGroupCount, extraAdGroupCount);
        System.arraycopy(extraPlayedAdGroups, 0, playedAdGroups, adGroupCount, extraAdGroupCount);
        timeBar.setAdGroupTimesMs(adGroupTimesMs, playedAdGroups, totalAdGroupCount);
    }
    updateProgress();
}
Also used : Player(com.google.android.exoplayer2.Player) SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) ForwardingPlayer(com.google.android.exoplayer2.ForwardingPlayer) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) Timeline(com.google.android.exoplayer2.Timeline) Nullable(androidx.annotation.Nullable) SuppressLint(android.annotation.SuppressLint)

Example 3 with TimeBar

use of com.google.android.exoplayer2.ui.TimeBar 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 4 with TimeBar

use of com.google.android.exoplayer2.ui.TimeBar 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)

Example 5 with TimeBar

use of com.google.android.exoplayer2.ui.TimeBar in project ExoPlayer by google.

the class StyledPlayerControlView 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);
    }
    if (enableRewind) {
        updateRewindButton();
    }
    if (enableFastForward) {
        updateFastForwardButton();
    }
    updateButton(enablePrevious, previousButton);
    updateButton(enableRewind, rewindButton);
    updateButton(enableFastForward, fastForwardButton);
    updateButton(enableNext, nextButton);
    if (timeBar != null) {
        timeBar.setEnabled(enableSeeking);
    }
}
Also used : Player(com.google.android.exoplayer2.Player) ForwardingPlayer(com.google.android.exoplayer2.ForwardingPlayer) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)12 Player (com.google.android.exoplayer2.Player)12 SuppressLint (android.annotation.SuppressLint)9 ForwardingPlayer (com.google.android.exoplayer2.ForwardingPlayer)9 SimpleExoPlayer (com.google.android.exoplayer2.SimpleExoPlayer)6 Timeline (com.google.android.exoplayer2.Timeline)4 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)3 TimeBar (com.google.android.exoplayer2.ui.TimeBar)2 Animator (android.animation.Animator)1 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)1 PendingIntent (android.app.PendingIntent)1 PictureInPictureParams (android.app.PictureInPictureParams)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 Parcelable (android.os.Parcelable)1 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)1 ViewGroup (android.view.ViewGroup)1 Window (android.view.Window)1 WindowInsetsController (android.view.WindowInsetsController)1