From 55939cf65aaca68a4dd0ad0e03e0ac09473d58ce Mon Sep 17 00:00:00 2001 From: anderslemke Date: Tue, 26 May 2020 13:11:56 +0200 Subject: [PATCH 1/9] Add flag on iOS --- ios/Video/RCTVideo.m | 14 ++++++++++++++ ios/Video/RCTVideoManager.m | 1 + 2 files changed, 15 insertions(+) diff --git a/ios/Video/RCTVideo.m b/ios/Video/RCTVideo.m index 8bfe702bd2..462a063aac 100644 --- a/ios/Video/RCTVideo.m +++ b/ios/Video/RCTVideo.m @@ -64,6 +64,7 @@ @implementation RCTVideo NSDictionary * _selectedAudioTrack; BOOL _playbackStalled; BOOL _playInBackground; + BOOL _preventsDisplaySleepDuringVideoPlayback; float _preferredForwardBufferDuration; BOOL _playWhenInactive; BOOL _pictureInPicture; @@ -106,6 +107,7 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher _controls = NO; _playerBufferEmpty = YES; _playInBackground = false; + _preventsDisplaySleepDuringVideoPlayback = true; _preferredForwardBufferDuration = 0.0f; _allowsExternalPlayback = YES; _playWhenInactive = false; @@ -818,6 +820,12 @@ - (void)setPlayInBackground:(BOOL)playInBackground _playInBackground = playInBackground; } +- (void)setPreventsDisplaySleepDuringVideoPlayback:(BOOL)preventsDisplaySleepDuringVideoPlayback +{ + _preventsDisplaySleepDuringVideoPlayback = preventsDisplaySleepDuringVideoPlayback; + [self applyModifiers]; +} + - (void)setAllowsExternalPlayback:(BOOL)allowsExternalPlayback { _allowsExternalPlayback = allowsExternalPlayback; @@ -1024,6 +1032,12 @@ - (void)applyModifiers [_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 3059ed79ad..62c8b821b3 100644 --- a/ios/Video/RCTVideoManager.m +++ b/ios/Video/RCTVideoManager.m @@ -32,6 +32,7 @@ - (dispatch_queue_t)methodQueue 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); From 6690d21409bee4a406d785fe981a208cb13a0ffa Mon Sep 17 00:00:00 2001 From: anderslemke Date: Tue, 26 May 2020 13:35:32 +0200 Subject: [PATCH 2/9] Add flag in Android --- .../java/com/brentvatne/react/ReactVideoView.java | 13 ++++++++++++- .../com/brentvatne/react/ReactVideoViewManager.java | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoView.java b/android/src/main/java/com/brentvatne/react/ReactVideoView.java index 89b4b89cdf..5ffdf7417a 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoView.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoView.java @@ -123,6 +123,7 @@ public String toString() { private boolean mRepeat = false; private boolean mPaused = false; private boolean mMuted = false; + private boolean mPreventsDisplaySleepDuringVideoPlayback = true; private float mVolume = 1.0f; private float mStereoPan = 0.0f; private float mProgressUpdateInterval = 250.0f; @@ -210,7 +211,6 @@ private void initializeMediaPlayerIfNeeded() { if (mMediaPlayer == null) { mMediaPlayerValid = false; mMediaPlayer = new MediaPlayer(); - mMediaPlayer.setScreenOnWhilePlaying(true); mMediaPlayer.setOnVideoSizeChangedListener(this); mMediaPlayer.setOnErrorListener(this); mMediaPlayer.setOnPreparedListener(this); @@ -421,6 +421,16 @@ private float calulateRelativeVolume() { return roundRelativeVolume.floatValue(); } + public void setPreventsDisplaySleepDuringVideoPlaybackModifier(final boolean preventsDisplaySleepDuringVideoPlayback) { + mPreventsDisplaySleepDuringVideoPlayback = preventsDisplaySleepDuringVideoPlayback; + + if (!mMediaPlayerValid) { + return; + } + + mMediaPlayer.setScreenOnWhilePlaying(mPreventsDisplaySleepDuringVideoPlayback); + } + public void setMutedModifier(final boolean muted) { mMuted = muted; @@ -517,6 +527,7 @@ public void applyModifiers() { setRepeatModifier(mRepeat); setPausedModifier(mPaused); setMutedModifier(mMuted); + setPreventsDisplaySleepDuringVideoPlaybackModifier(mPreventsDisplaySleepDuringVideoPlayback); setProgressUpdateInterval(mProgressUpdateInterval); setRateModifier(mRate); } diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java b/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java index dbf152a8e4..a67428b1e9 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java @@ -30,6 +30,7 @@ public class ReactVideoViewManager extends SimpleViewManager { 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"; From cbc78ab029328087e9ab893fdc52c2d9730d0043 Mon Sep 17 00:00:00 2001 From: anderslemke Date: Tue, 26 May 2020 13:40:50 +0200 Subject: [PATCH 3/9] Add documentation --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 9da556ebdb..621b2f60a1 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) @@ -611,6 +612,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. From 42bbecfebe577c71d34d9f820a761b679508ee51 Mon Sep 17 00:00:00 2001 From: anderslemke Date: Tue, 26 May 2020 13:41:56 +0200 Subject: [PATCH 4/9] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 880b695c4f..9a4d889c77 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) ### Version 5.1.0-alpha5 From 4876201dc967c8d101ee89c6259019d3e74ddc93 Mon Sep 17 00:00:00 2001 From: anderslemke Date: Tue, 26 May 2020 14:53:28 +0200 Subject: [PATCH 5/9] Also set setKeepScreenOn --- .../src/main/java/com/brentvatne/react/ReactVideoView.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoView.java b/android/src/main/java/com/brentvatne/react/ReactVideoView.java index 5ffdf7417a..f3f5fe7b07 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoView.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoView.java @@ -410,7 +410,7 @@ public void setPausedModifier(final boolean paused) { mProgressUpdateHandler.post(mProgressUpdateRunnable); } } - setKeepScreenOn(!mPaused); + setKeepScreenOn(!mPaused && mPreventsDisplaySleepDuringVideoPlayback); } // reduces the volume based on stereoPan @@ -429,6 +429,7 @@ public void setPreventsDisplaySleepDuringVideoPlaybackModifier(final boolean pre } mMediaPlayer.setScreenOnWhilePlaying(mPreventsDisplaySleepDuringVideoPlayback); + setKeepScreenOn(mPreventsDisplaySleepDuringVideoPlayback); } public void setMutedModifier(final boolean muted) { @@ -723,7 +724,7 @@ protected void onAttachedToWindow() { else { setSrc(mSrcUriString, mSrcType, mSrcIsNetwork, mSrcIsAsset, mRequestHeaders); } - setKeepScreenOn(true); + setKeepScreenOn(mPreventsDisplaySleepDuringVideoPlayback); } @Override From 966bbcd2272e3434596a8ce204bd0c7116fe3c69 Mon Sep 17 00:00:00 2001 From: Anton Tanderup Date: Thu, 28 May 2020 15:21:04 +0200 Subject: [PATCH 6/9] Fix prop not being set --- .../java/com/brentvatne/react/ReactVideoViewManager.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java b/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java index a67428b1e9..59efab497d 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java @@ -105,6 +105,11 @@ public void setSrc(final ReactVideoView videoView, @Nullable ReadableMap src) { } } + @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)]); From 5a46055c41543ce7bf126d0b7b8443da5cc91e54 Mon Sep 17 00:00:00 2001 From: Anton Tanderup Date: Wed, 3 Jun 2020 14:33:22 +0200 Subject: [PATCH 7/9] add preventsDisplaySleepDuringVideoPlayback to exoplayer --- .../brentvatne/exoplayer/ReactExoplayerView.java | 14 ++++++++++++-- .../exoplayer/ReactExoplayerViewManager.java | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) 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 1c487ae609..1a5d9a9da6 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -139,6 +139,7 @@ class ReactExoplayerView extends FrameLayout implements private Dynamic textTrackValue; private ReadableArray textTracks; private boolean disableFocus; + private boolean preventsDisplaySleepDuringVideoPlayback; private float mProgressUpdateInterval = 250.0f; private boolean playInBackground = false; private Map requestHeaders; @@ -605,7 +606,7 @@ private void startPlayback() { initializePlayer(); } if (!disableFocus) { - setKeepScreenOn(true); + setKeepScreenOn(true && preventsDisplaySleepDuringVideoPlayback); } } @@ -629,7 +630,6 @@ private void onStopPlayback() { //If the video is in fullscreen, then we will update the video to normal mode. setFullscreen(!isFullscreen); } - setKeepScreenOn(false); audioManager.abandonAudioFocus(this); enableFullScreenButton(false); } @@ -714,11 +714,15 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { 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"; @@ -731,11 +735,13 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { playerControlView.show(); } enableFullScreenButton(true); + setKeepScreenOn(preventsDisplaySleepDuringVideoPlayback); break; case Player.STATE_ENDED: text += "ended"; eventEmitter.end(); onStopPlayback(); + setKeepScreenOn(false); break; default: text += "unknown"; @@ -1048,6 +1054,10 @@ public void setRepeatModifier(boolean repeat) { 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 c7c9634644..cf50fdaecd 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 Date: Fri, 12 Jun 2020 12:06:58 +0200 Subject: [PATCH 8/9] Update android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java --- .../main/java/com/brentvatne/exoplayer/ReactExoplayerView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1a5d9a9da6..499dc2d4b9 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -606,7 +606,7 @@ private void startPlayback() { initializePlayer(); } if (!disableFocus) { - setKeepScreenOn(true && preventsDisplaySleepDuringVideoPlayback); + setKeepScreenOn(preventsDisplaySleepDuringVideoPlayback); } } From e6e37b8da27ea3f4ad4c1cfd6fe66a6f0991516a Mon Sep 17 00:00:00 2001 From: Anders Lemke Date: Tue, 16 Jun 2020 13:49:23 +0200 Subject: [PATCH 9/9] Update android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java Co-authored-by: Jens Andersson --- .../main/java/com/brentvatne/exoplayer/ReactExoplayerView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 499dc2d4b9..2511095b35 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -139,7 +139,7 @@ class ReactExoplayerView extends FrameLayout implements private Dynamic textTrackValue; private ReadableArray textTracks; private boolean disableFocus; - private boolean preventsDisplaySleepDuringVideoPlayback; + private boolean preventsDisplaySleepDuringVideoPlayback = true; private float mProgressUpdateInterval = 250.0f; private boolean playInBackground = false; private Map requestHeaders;