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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With