Android: MediaPlayer - Prevent screen from dimming

This commit is contained in:
Gerardo Pacheco 2018-07-11 12:28:26 +02:00
parent 240444c213
commit 2f0b694b38

View File

@ -358,10 +358,12 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
if (mPaused) { if (mPaused) {
if (mMediaPlayer.isPlaying()) { if (mMediaPlayer.isPlaying()) {
pause(); pause();
setPreventScreenFromDimmingFlag(false);
} }
} else { } else {
if (!mMediaPlayer.isPlaying()) { if (!mMediaPlayer.isPlaying()) {
start(); start();
setPreventScreenFromDimmingFlag(true);
// Setting the rate unpauses, so we have to wait for an unpause // Setting the rate unpauses, so we have to wait for an unpause
if (mRate != mActiveRate) { if (mRate != mActiveRate) {
setRateModifier(mRate); setRateModifier(mRate);
@ -457,6 +459,42 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
this.mUseNativeControls = controls; this.mUseNativeControls = controls;
} }
public boolean isPreventScreenFromDimmingFlagOn() {
int flags = mThemedReactContext.getCurrentActivity().getWindow().getAttributes().flags;
if ((flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) == 0) {
return false;
}
return true;
}
public void setPreventScreenFromDimmingFlag(final boolean state) {
if (!mMediaPlayerValid && mThemedReactContext == null) {
return;
}
final boolean isFlagOn = isPreventScreenFromDimmingFlagOn();
if (state && !isFlagOn) {
mThemedReactContext.getCurrentActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mThemedReactContext.getCurrentActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
if (!state && isFlagOn) {
mThemedReactContext.getCurrentActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mThemedReactContext.getCurrentActivity().getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
}
@Override @Override
public void onPrepared(MediaPlayer mp) { public void onPrepared(MediaPlayer mp) {
@ -592,14 +630,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
mMediaPlayerValid = false; mMediaPlayerValid = false;
super.onDetachedFromWindow(); super.onDetachedFromWindow();
if (mThemedReactContext != null) { setPreventScreenFromDimmingFlag(false);
mThemedReactContext.getCurrentActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mThemedReactContext.getCurrentActivity().getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
} }
@Override @Override
@ -607,14 +638,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
super.onAttachedToWindow(); super.onAttachedToWindow();
if (mThemedReactContext != null) { setPreventScreenFromDimmingFlag(true);
mThemedReactContext.getCurrentActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mThemedReactContext.getCurrentActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
if(mMainVer>0) { if(mMainVer>0) {
setSrc(mSrcUriString, mSrcType, mSrcIsNetwork, mSrcIsAsset, mRequestHeaders, mMainVer, mPatchVer); setSrc(mSrcUriString, mSrcType, mSrcIsNetwork, mSrcIsAsset, mRequestHeaders, mMainVer, mPatchVer);