Search in sources :

Example 81 with Player

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

the class StyledPlayerView method updateAspectRatio.

private void updateAspectRatio() {
    VideoSize videoSize = player != null ? player.getVideoSize() : VideoSize.UNKNOWN;
    int width = videoSize.width;
    int height = videoSize.height;
    int unappliedRotationDegrees = videoSize.unappliedRotationDegrees;
    float videoAspectRatio = (height == 0 || width == 0) ? 0 : (width * videoSize.pixelWidthHeightRatio) / height;
    if (surfaceView instanceof TextureView) {
        // Try to apply rotation transformation when our surface is a TextureView.
        if (videoAspectRatio > 0 && (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270)) {
            // We will apply a rotation 90/270 degree to the output texture of the TextureView.
            // In this case, the output video's width and height will be swapped.
            videoAspectRatio = 1 / videoAspectRatio;
        }
        if (textureViewRotation != 0) {
            surfaceView.removeOnLayoutChangeListener(componentListener);
        }
        textureViewRotation = unappliedRotationDegrees;
        if (textureViewRotation != 0) {
            // The texture view's dimensions might be changed after layout step.
            // So add an OnLayoutChangeListener to apply rotation after layout step.
            surfaceView.addOnLayoutChangeListener(componentListener);
        }
        applyTextureViewRotation((TextureView) surfaceView, textureViewRotation);
    }
    onContentAspectRatioChanged(contentFrame, surfaceViewIgnoresVideoAspectRatio ? 0 : videoAspectRatio);
}
Also used : VideoSize(com.google.android.exoplayer2.video.VideoSize) TextureView(android.view.TextureView) SuppressLint(android.annotation.SuppressLint)

Example 82 with Player

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

the class StyledPlayerView method updateForCurrentTrackSelections.

private void updateForCurrentTrackSelections(boolean isNewPlayer) {
    @Nullable Player player = this.player;
    if (player == null || player.getCurrentTracksInfo().getTrackGroupInfos().isEmpty()) {
        if (!keepContentOnPlayerReset) {
            hideArtwork();
            closeShutter();
        }
        return;
    }
    if (isNewPlayer && !keepContentOnPlayerReset) {
        // Hide any video from the previous player.
        closeShutter();
    }
    if (player.getCurrentTracksInfo().isTypeSelected(C.TRACK_TYPE_VIDEO)) {
        // Video enabled, so artwork must be hidden. If the shutter is closed, it will be opened
        // in onRenderedFirstFrame().
        hideArtwork();
        return;
    }
    // Video disabled so the shutter must be closed.
    closeShutter();
    // Display artwork if enabled and available, else hide it.
    if (useArtwork()) {
        if (setArtworkFromMediaMetadata(player.getMediaMetadata())) {
            return;
        }
        if (setDrawableArtwork(defaultArtwork)) {
            return;
        }
    }
    // Artwork disabled or unavailable.
    hideArtwork();
}
Also used : Player(com.google.android.exoplayer2.Player) Nullable(androidx.annotation.Nullable)

Example 83 with Player

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

the class PlayerView method updateAspectRatio.

private void updateAspectRatio() {
    VideoSize videoSize = player != null ? player.getVideoSize() : VideoSize.UNKNOWN;
    int width = videoSize.width;
    int height = videoSize.height;
    int unappliedRotationDegrees = videoSize.unappliedRotationDegrees;
    float videoAspectRatio = (height == 0 || width == 0) ? 0 : (width * videoSize.pixelWidthHeightRatio) / height;
    if (surfaceView instanceof TextureView) {
        // Try to apply rotation transformation when our surface is a TextureView.
        if (videoAspectRatio > 0 && (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270)) {
            // We will apply a rotation 90/270 degree to the output texture of the TextureView.
            // In this case, the output video's width and height will be swapped.
            videoAspectRatio = 1 / videoAspectRatio;
        }
        if (textureViewRotation != 0) {
            surfaceView.removeOnLayoutChangeListener(componentListener);
        }
        textureViewRotation = unappliedRotationDegrees;
        if (textureViewRotation != 0) {
            // The texture view's dimensions might be changed after layout step.
            // So add an OnLayoutChangeListener to apply rotation after layout step.
            surfaceView.addOnLayoutChangeListener(componentListener);
        }
        applyTextureViewRotation((TextureView) surfaceView, textureViewRotation);
    }
    onContentAspectRatioChanged(contentFrame, surfaceViewIgnoresVideoAspectRatio ? 0 : videoAspectRatio);
}
Also used : VideoSize(com.google.android.exoplayer2.video.VideoSize) TextureView(android.view.TextureView) SuppressLint(android.annotation.SuppressLint)

Example 84 with Player

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

the class PlayerView method setPlayer.

/**
 * Sets the {@link Player} to use.
 *
 * <p>To transition a {@link Player} from targeting one view to another, it's recommended to use
 * {@link #switchTargetView(Player, PlayerView, PlayerView)} rather than this method. If you do
 * wish to use this method directly, be sure to attach the player to the new view <em>before</em>
 * calling {@code setPlayer(null)} to detach it from the old one. This ordering is significantly
 * more efficient and may allow for more seamless transitions.
 *
 * @param player The {@link Player} to use, or {@code null} to detach the current player. Only
 *     players which are accessed on the main thread are supported ({@code
 *     player.getApplicationLooper() == Looper.getMainLooper()}).
 */
public void setPlayer(@Nullable Player player) {
    Assertions.checkState(Looper.myLooper() == Looper.getMainLooper());
    Assertions.checkArgument(player == null || player.getApplicationLooper() == Looper.getMainLooper());
    if (this.player == player) {
        return;
    }
    @Nullable Player oldPlayer = this.player;
    if (oldPlayer != null) {
        oldPlayer.removeListener(componentListener);
        if (oldPlayer.isCommandAvailable(COMMAND_SET_VIDEO_SURFACE)) {
            if (surfaceView instanceof TextureView) {
                oldPlayer.clearVideoTextureView((TextureView) surfaceView);
            } else if (surfaceView instanceof SurfaceView) {
                oldPlayer.clearVideoSurfaceView((SurfaceView) surfaceView);
            }
        }
    }
    if (subtitleView != null) {
        subtitleView.setCues(null);
    }
    this.player = player;
    if (useController()) {
        controller.setPlayer(player);
    }
    updateBuffering();
    updateErrorMessage();
    updateForCurrentTrackSelections(/* isNewPlayer= */
    true);
    if (player != null) {
        if (player.isCommandAvailable(COMMAND_SET_VIDEO_SURFACE)) {
            if (surfaceView instanceof TextureView) {
                player.setVideoTextureView((TextureView) surfaceView);
            } else if (surfaceView instanceof SurfaceView) {
                player.setVideoSurfaceView((SurfaceView) surfaceView);
            }
            updateAspectRatio();
        }
        if (subtitleView != null && player.isCommandAvailable(COMMAND_GET_TEXT)) {
            subtitleView.setCues(player.getCurrentCues());
        }
        player.addListener(componentListener);
        maybeShowController(false);
    } else {
        hideController();
    }
}
Also used : Player(com.google.android.exoplayer2.Player) TextureView(android.view.TextureView) Nullable(androidx.annotation.Nullable) SurfaceView(android.view.SurfaceView) GLSurfaceView(android.opengl.GLSurfaceView)

Example 85 with Player

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

the class PlayerView method updateForCurrentTrackSelections.

private void updateForCurrentTrackSelections(boolean isNewPlayer) {
    @Nullable Player player = this.player;
    if (player == null || !player.isCommandAvailable(Player.COMMAND_GET_TRACK_INFOS) || player.getCurrentTracksInfo().getTrackGroupInfos().isEmpty()) {
        if (!keepContentOnPlayerReset) {
            hideArtwork();
            closeShutter();
        }
        return;
    }
    if (isNewPlayer && !keepContentOnPlayerReset) {
        // Hide any video from the previous player.
        closeShutter();
    }
    if (player.getCurrentTracksInfo().isTypeSelected(C.TRACK_TYPE_VIDEO)) {
        // Video enabled, so artwork must be hidden. If the shutter is closed, it will be opened
        // in onRenderedFirstFrame().
        hideArtwork();
        return;
    }
    // Video disabled so the shutter must be closed.
    closeShutter();
    // Display artwork if enabled and available, else hide it.
    if (useArtwork()) {
        if (setArtworkFromMediaMetadata(player.getMediaMetadata())) {
            return;
        }
        if (setDrawableArtwork(defaultArtwork)) {
            return;
        }
    }
    // Artwork disabled or unavailable.
    hideArtwork();
}
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