Issue
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?
Solution
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.
Answered By - Vignesh Sundaramoorthy
Answer Checked By - Candace Johnson (JavaFixing Volunteer)