Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support preventsDisplaySleepDuringVideoPlayback #2019

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ var styles = StyleSheet.create({
* [poster](#poster)
* [posterResizeMode](#posterresizemode)
* [preferredForwardBufferDuration](#preferredForwardBufferDuration)
* [preventsDisplaySleepDuringVideoPlayback](#preventsDisplaySleepDuringVideoPlayback)
* [progressUpdateInterval](#progressupdateinterval)
* [rate](#rate)
* [repeat](#repeat)
Expand Down Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class ReactExoplayerView extends FrameLayout implements
private Dynamic textTrackValue;
private ReadableArray textTracks;
private boolean disableFocus;
private boolean preventsDisplaySleepDuringVideoPlayback;
anderslemke marked this conversation as resolved.
Show resolved Hide resolved
private float mProgressUpdateInterval = 250.0f;
private boolean playInBackground = false;
private Map<String, String> requestHeaders;
Expand Down Expand Up @@ -605,7 +606,7 @@ private void startPlayback() {
initializePlayer();
}
if (!disableFocus) {
setKeepScreenOn(true);
setKeepScreenOn(true && preventsDisplaySleepDuringVideoPlayback);
anderslemke marked this conversation as resolved.
Show resolved Hide resolved
anderslemke marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
private static final String PROP_BUFFER_CONFIG_MAX_BUFFER_MS = "maxBufferMs";
private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_MS = "bufferForPlaybackMs";
private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS = "bufferForPlaybackAfterRebufferMs";
private static final String PROP_PREVENTS_DISPLAY_SLEEP_DURING_VIDEO_PLAYBACK = "preventsDisplaySleepDuringVideoPlayback";
private static final String PROP_PROGRESS_UPDATE_INTERVAL = "progressUpdateInterval";
private static final String PROP_REPORT_BANDWIDTH = "reportBandwidth";
private static final String PROP_SEEK = "seek";
Expand Down Expand Up @@ -150,6 +151,11 @@ public void setRepeat(final ReactExoplayerView videoView, final boolean repeat)
videoView.setRepeatModifier(repeat);
}

@ReactProp(name = PROP_PREVENTS_DISPLAY_SLEEP_DURING_VIDEO_PLAYBACK, defaultBoolean = false)
public void setPreventsDisplaySleepDuringVideoPlayback(final ReactExoplayerView videoView, final boolean preventsSleep) {
videoView.setPreventsDisplaySleepDuringVideoPlayback(preventsSleep);
}

@ReactProp(name = PROP_SELECTED_VIDEO_TRACK)
public void setSelectedVideoTrack(final ReactExoplayerView videoView,
@Nullable ReadableMap selectedVideoTrack) {
Expand Down
18 changes: 15 additions & 3 deletions android/src/main/java/com/brentvatne/react/ReactVideoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -410,7 +410,7 @@ public void setPausedModifier(final boolean paused) {
mProgressUpdateHandler.post(mProgressUpdateRunnable);
}
}
setKeepScreenOn(!mPaused);
setKeepScreenOn(!mPaused && mPreventsDisplaySleepDuringVideoPlayback);
}

// reduces the volume based on stereoPan
Expand All @@ -421,6 +421,17 @@ private float calulateRelativeVolume() {
return roundRelativeVolume.floatValue();
}

public void setPreventsDisplaySleepDuringVideoPlaybackModifier(final boolean preventsDisplaySleepDuringVideoPlayback) {
mPreventsDisplaySleepDuringVideoPlayback = preventsDisplaySleepDuringVideoPlayback;

if (!mMediaPlayerValid) {
return;
}

mMediaPlayer.setScreenOnWhilePlaying(mPreventsDisplaySleepDuringVideoPlayback);
setKeepScreenOn(mPreventsDisplaySleepDuringVideoPlayback);
}

public void setMutedModifier(final boolean muted) {
mMuted = muted;

Expand Down Expand Up @@ -517,6 +528,7 @@ public void applyModifiers() {
setRepeatModifier(mRepeat);
setPausedModifier(mPaused);
setMutedModifier(mMuted);
setPreventsDisplaySleepDuringVideoPlaybackModifier(mPreventsDisplaySleepDuringVideoPlayback);
setProgressUpdateInterval(mProgressUpdateInterval);
setRateModifier(mRate);
}
Expand Down Expand Up @@ -712,7 +724,7 @@ protected void onAttachedToWindow() {
else {
setSrc(mSrcUriString, mSrcType, mSrcIsNetwork, mSrcIsAsset, mRequestHeaders);
}
setKeepScreenOn(true);
setKeepScreenOn(mPreventsDisplaySleepDuringVideoPlayback);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ReactVideoViewManager extends SimpleViewManager<ReactVideoView> {
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";
Expand Down Expand Up @@ -104,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)]);
Expand Down
14 changes: 14 additions & 0 deletions ios/Video/RCTVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ @implementation RCTVideo
NSDictionary * _selectedAudioTrack;
BOOL _playbackStalled;
BOOL _playInBackground;
BOOL _preventsDisplaySleepDuringVideoPlayback;
float _preferredForwardBufferDuration;
BOOL _playWhenInactive;
BOOL _pictureInPicture;
Expand Down Expand Up @@ -106,6 +107,7 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
_controls = NO;
_playerBufferEmpty = YES;
_playInBackground = false;
_preventsDisplaySleepDuringVideoPlayback = true;
_preferredForwardBufferDuration = 0.0f;
_allowsExternalPlayback = YES;
_playWhenInactive = false;
Expand Down Expand Up @@ -818,6 +820,12 @@ - (void)setPlayInBackground:(BOOL)playInBackground
_playInBackground = playInBackground;
}

- (void)setPreventsDisplaySleepDuringVideoPlayback:(BOOL)preventsDisplaySleepDuringVideoPlayback
{
_preventsDisplaySleepDuringVideoPlayback = preventsDisplaySleepDuringVideoPlayback;
[self applyModifiers];
}

- (void)setAllowsExternalPlayback:(BOOL)allowsExternalPlayback
{
_allowsExternalPlayback = allowsExternalPlayback;
Expand Down Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions ios/Video/RCTVideoManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down