From 8962720f56719b89840e7f3ac5924532937fa08f Mon Sep 17 00:00:00 2001 From: Anders Lemke Date: Tue, 16 Jun 2020 14:31:23 +0200 Subject: [PATCH] Support preventsDisplaySleepDuringVideoPlayback (#2019) * Add flag on iOS * Add flag in Android * Add documentation * Add changelog entry * Also set setKeepScreenOn * Fix prop not being set * add preventsDisplaySleepDuringVideoPlayback to exoplayer * Update android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java * Update android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java Co-authored-by: Jens Andersson Co-authored-by: Anton Tanderup Co-authored-by: Jens Andersson --- CHANGELOG.md | 1 + README.md | 8 ++++++++ .../exoplayer/ReactExoplayerView.java | 14 ++++++++++++-- .../exoplayer/ReactExoplayerViewManager.java | 6 ++++++ .../com/brentvatne/react/ReactVideoView.java | 18 +++++++++++++++--- .../react/ReactVideoViewManager.java | 6 ++++++ ios/Video/RCTVideo.m | 14 ++++++++++++++ ios/Video/RCTVideoManager.m | 1 + 8 files changed, 63 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd7a97c6..5fefee0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Added `preferredForwardBufferDuration` (iOS) - the duration the player should buffer media from the network ahead of the playhead to guard against playback disruption. (#1944) - Added `currentPlaybackTime` (Android ExoPlayer, iOS) - when playing an HLS live stream with a `EXT-X-PROGRAM-DATE-TIME` tag configured, then this property will contain the epoch value in msec. (#1944) - Added `trackId` (Android ExoPlayer) - Configure an identifier for the video stream to link the playback context to the events emitted. (#1944) +- Added preventsDisplaySleepDuringVideoPlayback (#2019) - Reverted the JS fullscreening for Android. [#2013](https://github.com/react-native-community/react-native-video/pull/2013) - Set iOS request headers without needing to edit RCTVideo.m. [#2014](https://github.com/react-native-community/react-native-video/pull/2014) diff --git a/README.md b/README.md index 29a20e70..00fc7932 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,7 @@ var styles = StyleSheet.create({ * [poster](#poster) * [posterResizeMode](#posterresizemode) * [preferredForwardBufferDuration](#preferredForwardBufferDuration) +* [preventsDisplaySleepDuringVideoPlayback](#preventsDisplaySleepDuringVideoPlayback) * [progressUpdateInterval](#progressupdateinterval) * [rate](#rate) * [repeat](#repeat) @@ -607,6 +608,13 @@ Default: 0 Platforms: iOS +#### preventsDisplaySleepDuringVideoPlayback +Controls whether or not the display should be allowed to sleep while playing the video. Default is not to allow display to sleep. + +Default: true + +Platforms: iOS, Android + #### progressUpdateInterval Delay in milliseconds between onProgress events in milliseconds. diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index efc04092..5df1f3bb 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -136,6 +136,7 @@ class ReactExoplayerView extends FrameLayout implements private Dynamic textTrackValue; private ReadableArray textTracks; private boolean disableFocus; + private boolean preventsDisplaySleepDuringVideoPlayback = true; private float mProgressUpdateInterval = 250.0f; private boolean playInBackground = false; private Map requestHeaders; @@ -564,7 +565,7 @@ class ReactExoplayerView extends FrameLayout implements initializePlayer(); } if (!disableFocus) { - setKeepScreenOn(true); + setKeepScreenOn(preventsDisplaySleepDuringVideoPlayback); } } @@ -586,7 +587,6 @@ class ReactExoplayerView extends FrameLayout implements if (isFullscreen) { setFullscreen(false); } - setKeepScreenOn(false); audioManager.abandonAudioFocus(this); } @@ -670,11 +670,15 @@ class ReactExoplayerView extends FrameLayout implements text += "idle"; eventEmitter.idle(); clearProgressMessageHandler(); + if (!playWhenReady) { + setKeepScreenOn(false); + } break; case Player.STATE_BUFFERING: text += "buffering"; onBuffering(true); clearProgressMessageHandler(); + setKeepScreenOn(preventsDisplaySleepDuringVideoPlayback); break; case Player.STATE_READY: text += "ready"; @@ -686,11 +690,13 @@ class ReactExoplayerView extends FrameLayout implements if (playerControlView != null) { playerControlView.show(); } + setKeepScreenOn(preventsDisplaySleepDuringVideoPlayback); break; case Player.STATE_ENDED: text += "ended"; eventEmitter.end(); onStopPlayback(); + setKeepScreenOn(false); break; default: text += "unknown"; @@ -1003,6 +1009,10 @@ class ReactExoplayerView extends FrameLayout implements this.repeat = repeat; } + public void setPreventsDisplaySleepDuringVideoPlayback(boolean preventsDisplaySleepDuringVideoPlayback) { + this.preventsDisplaySleepDuringVideoPlayback = preventsDisplaySleepDuringVideoPlayback; + } + public void setSelectedTrack(int trackType, String type, Dynamic value) { if (player == null) return; int rendererIndex = getTrackRendererIndex(trackType); diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java index c7c96346..cf50fdae 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java @@ -44,6 +44,7 @@ public class ReactExoplayerViewManager extends ViewGroupManager { public static final String PROP_REPEAT = "repeat"; public static final String PROP_PAUSED = "paused"; public static final String PROP_MUTED = "muted"; + public static final String PROP_PREVENTS_DISPLAY_SLEEP_DURING_VIDEO_PLAYBACK = "preventsDisplaySleepDuringVideoPlayback"; public static final String PROP_VOLUME = "volume"; public static final String PROP_STEREO_PAN = "stereoPan"; public static final String PROP_PROGRESS_UPDATE_INTERVAL = "progressUpdateInterval"; @@ -104,6 +105,11 @@ public class ReactVideoViewManager extends SimpleViewManager { } } + @ReactProp(name = PROP_PREVENTS_DISPLAY_SLEEP_DURING_VIDEO_PLAYBACK) + public void setPropPreventsDisplaySleepDuringVideoPlayback(final ReactVideoView videoView, final boolean doPreventSleep) { + videoView.setPreventsDisplaySleepDuringVideoPlaybackModifier(doPreventSleep); + } + @ReactProp(name = PROP_RESIZE_MODE) public void setResizeMode(final ReactVideoView videoView, final String resizeModeOrdinalString) { videoView.setResizeModeModifier(ScalableType.values()[Integer.parseInt(resizeModeOrdinalString)]); diff --git a/ios/Video/RCTVideo.m b/ios/Video/RCTVideo.m index a24a33de..01e1b1c6 100644 --- a/ios/Video/RCTVideo.m +++ b/ios/Video/RCTVideo.m @@ -64,6 +64,7 @@ static int const RCTVideoUnset = -1; NSDictionary * _selectedAudioTrack; BOOL _playbackStalled; BOOL _playInBackground; + BOOL _preventsDisplaySleepDuringVideoPlayback; float _preferredForwardBufferDuration; BOOL _playWhenInactive; BOOL _pictureInPicture; @@ -106,6 +107,7 @@ static int const RCTVideoUnset = -1; _controls = NO; _playerBufferEmpty = YES; _playInBackground = false; + _preventsDisplaySleepDuringVideoPlayback = true; _preferredForwardBufferDuration = 0.0f; _allowsExternalPlayback = YES; _playWhenInactive = false; @@ -815,6 +817,12 @@ static int const RCTVideoUnset = -1; _playInBackground = playInBackground; } +- (void)setPreventsDisplaySleepDuringVideoPlayback:(BOOL)preventsDisplaySleepDuringVideoPlayback +{ + _preventsDisplaySleepDuringVideoPlayback = preventsDisplaySleepDuringVideoPlayback; + [self applyModifiers]; +} + - (void)setAllowsExternalPlayback:(BOOL)allowsExternalPlayback { _allowsExternalPlayback = allowsExternalPlayback; @@ -1021,6 +1029,12 @@ static int const RCTVideoUnset = -1; [_player setVolume:_volume]; [_player setMuted:NO]; } + + if (@available(iOS 12.0, *)) { + self->_player.preventsDisplaySleepDuringVideoPlayback = _preventsDisplaySleepDuringVideoPlayback; + } else { + // Fallback on earlier versions + } [self setMaxBitRate:_maxBitRate]; [self setSelectedAudioTrack:_selectedAudioTrack]; diff --git a/ios/Video/RCTVideoManager.m b/ios/Video/RCTVideoManager.m index 3059ed79..62c8b821 100644 --- a/ios/Video/RCTVideoManager.m +++ b/ios/Video/RCTVideoManager.m @@ -32,6 +32,7 @@ RCT_EXPORT_VIEW_PROPERTY(muted, BOOL); RCT_EXPORT_VIEW_PROPERTY(controls, BOOL); RCT_EXPORT_VIEW_PROPERTY(volume, float); RCT_EXPORT_VIEW_PROPERTY(playInBackground, BOOL); +RCT_EXPORT_VIEW_PROPERTY(preventsDisplaySleepDuringVideoPlayback, BOOL); RCT_EXPORT_VIEW_PROPERTY(preferredForwardBufferDuration, float); RCT_EXPORT_VIEW_PROPERTY(playWhenInactive, BOOL); RCT_EXPORT_VIEW_PROPERTY(pictureInPicture, BOOL);