Skip to content

Commit

Permalink
fix(android): keep screen on on fullscreen (#3563)
Browse files Browse the repository at this point in the history
add KEEP_SCREEN_ON flag when video is fullscreen & playing to avoid phone to go off.
inspired by expo/expo@2d84661
  • Loading branch information
gkueny authored Mar 22, 2024
1 parent d5c8b51 commit bfb76e6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ private void setVideoView() {
}
}

public boolean isPlaying() {
return player != null && player.isPlaying();
}

public void setSubtitleStyle(SubtitleStyle style) {
// ensure we reset subtile style before reapplying it
subtitleLayout.setUserDefaultStyle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,73 @@
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import androidx.activity.OnBackPressedCallback;
import androidx.media3.ui.LegacyPlayerControlView;

import com.brentvatne.common.toolbox.DebugLog;

import java.lang.ref.WeakReference;

@SuppressLint("PrivateResource")
public class FullScreenPlayerView extends Dialog {
private final LegacyPlayerControlView playerControlView;
private final ExoPlayerView exoPlayerView;
private final ReactExoplayerView reactExoplayerView;
private ViewGroup parent;
private final FrameLayout containerView;
private final OnBackPressedCallback onBackPressedCallback;
private final Handler mKeepScreenOnHandler;
private final Runnable mKeepScreenOnUpdater;

private static class KeepScreenOnUpdater implements Runnable {
private final static long UPDATE_KEEP_SCREEN_ON_FLAG_MS = 200;
private final WeakReference<FullScreenPlayerView> mFullscreenPlayer;

KeepScreenOnUpdater(FullScreenPlayerView player) {
mFullscreenPlayer = new WeakReference<>(player);
}

@Override
public void run() {
try {
FullScreenPlayerView fullscreenVideoPlayer = mFullscreenPlayer.get();
if (fullscreenVideoPlayer != null) {
final Window window = fullscreenVideoPlayer.getWindow();
if (window != null) {
boolean isPlaying = fullscreenVideoPlayer.exoPlayerView.isPlaying();
if (isPlaying) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
fullscreenVideoPlayer.mKeepScreenOnHandler.postDelayed(this, UPDATE_KEEP_SCREEN_ON_FLAG_MS);
}
} catch (Exception ex) {
DebugLog.e("ExoPlayer Exception", "Failed to flag FLAG_KEEP_SCREEN_ON on fullscreeen.");
DebugLog.e("ExoPlayer Exception", ex.toString());
}
}
}

public FullScreenPlayerView(Context context, ExoPlayerView exoPlayerView, LegacyPlayerControlView playerControlView, OnBackPressedCallback onBackPressedCallback) {
public FullScreenPlayerView(Context context, ExoPlayerView exoPlayerView, ReactExoplayerView reactExoplayerView, LegacyPlayerControlView playerControlView, OnBackPressedCallback onBackPressedCallback) {
super(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
this.playerControlView = playerControlView;
this.exoPlayerView = exoPlayerView;
this.reactExoplayerView = reactExoplayerView;
this.onBackPressedCallback = onBackPressedCallback;
containerView = new FrameLayout(context);
setContentView(containerView, generateDefaultLayoutParams());

mKeepScreenOnUpdater = new KeepScreenOnUpdater(this);
mKeepScreenOnHandler = new Handler();
}

@Override
Expand Down Expand Up @@ -53,6 +98,7 @@ protected void onStart() {

@Override
protected void onStop() {
mKeepScreenOnHandler.removeCallbacks(mKeepScreenOnUpdater);
containerView.removeView(exoPlayerView);
parent.addView(exoPlayerView, generateDefaultLayoutParams());

Expand All @@ -70,6 +116,15 @@ protected void onStop() {
super.onStop();
}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();

if (reactExoplayerView.getPreventsDisplaySleepDuringVideoPlayback()) {
mKeepScreenOnHandler.post(mKeepScreenOnUpdater);
}
}

private FrameLayout.LayoutParams generateDefaultLayoutParams() {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ private void initializePlayerControl() {
}

if (fullScreenPlayerView == null) {
fullScreenPlayerView = new FullScreenPlayerView(getContext(), exoPlayerView, playerControlView, new OnBackPressedCallback(true) {
fullScreenPlayerView = new FullScreenPlayerView(getContext(), exoPlayerView, this, playerControlView, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
setFullscreen(false);
Expand Down Expand Up @@ -1966,6 +1966,10 @@ public void setDisableBuffering(boolean disableBuffering) {
this.disableBuffering = disableBuffering;
}

public boolean getPreventsDisplaySleepDuringVideoPlayback() {
return preventsDisplaySleepDuringVideoPlayback;
}

private void updateFullScreenButtonVisbility() {
if (playerControlView != null) {
final ImageButton fullScreenButton = playerControlView.findViewById(R.id.exo_fullscreen);
Expand Down
1 change: 1 addition & 0 deletions examples/basic/src/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ class VideoPlayer extends Component {
selectedTextTrack={this.state.selectedTextTrack}
selectedAudioTrack={this.state.selectedAudioTrack}
playInBackground={false}
preventsDisplaySleepDuringVideoPlayback={true}
/>
</TouchableOpacity>
);
Expand Down

0 comments on commit bfb76e6

Please sign in to comment.