Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exoplayer 2 prevent screen dim on video playback

I have simple player activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.epix.presentation.player.PlayerActivity">

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

When the video is running the screen will dim, I have to tap on the screen for it to get brighter again. I've tried setting android:keepScreenOn="true", this of course does not help because the screen does not turn off it just dims.

Is there a way to prevent screen dimming for happening?

like image 563
Calin Avatar asked Apr 04 '18 18:04

Calin


2 Answers

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.

like image 73
Vignesh Sundaramoorthy Avatar answered Sep 28 '22 15:09

Vignesh Sundaramoorthy


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.

like image 41
marcbaechinger Avatar answered Sep 28 '22 15:09

marcbaechinger