Exoplayer 2 prevent screen dim on video playback

if you get problem, video view is dimmed on dialog fragment or else. you can use this in attribute:

app:surface_type="texture_view"

its work for me


Hope this will help someone.

Setting setKeepScreenOn while video playing & buffering only, & allowing screen to sleep when the video is paused/not played/any error happened.

playerView = exoPlayerLayout.findViewById(R.id.exo_player_view);
player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(context),
                new DefaultTrackSelector(), new DefaultLoadControl());

player.addListener(new PlayerEventListener());

PlayerEventListener

private class PlayerEventListener implements Player.EventListener {
    @Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        if (playbackState == Player.STATE_IDLE || playbackState == Player.STATE_ENDED ||
                !playWhenReady) {

            playerView.setKeepScreenOn(false);
        } else { // STATE_READY, STATE_BUFFERING
            // This prevents the screen from getting dim/lock
            playerView.setKeepScreenOn(true);
        }
    }
    
    @Override
    public void onTimelineChanged(Timeline timeline, Object manifest) {}

    @Override
    public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {}

    @Override
    public void onLoadingChanged(boolean isLoading) {}

    @Override
    public void onRepeatModeChanged(int repeatMode) { }

    @Override
    public void onPlayerError(ExoPlaybackException error) { }

    @Override
    public void onPositionDiscontinuity() { }

    @Override
    public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) { }
}

Check the playback states here, if you want to modify setKeepScreenOn based on your needs.


Easiest is to have the keepScreenOn attribute with a value of true in your ConstraintLayout element:

android:keepScreenOn="true"

You may choose to do this programmatically for instance in the onCreate method of your activity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

There is a page about the topic of keeping the screen on on developer.android.com.