I am trying to do some picture in picture mode using react-native. I wrote a react module
I need to generate something similar to this but inside the react native module
public class MainActivity extends AppCompatActivity {
private PlayerView playerView;
private Player player;
private boolean playerShouldPause = true;
...
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
// Hiding the ActionBar
if (isInPictureInPictureMode) {
getSupportActionBar().hide();
} else {
getSupportActionBar().show();
}
playerView.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
}
...
}
There is some way to do it the same way but inside ReactContextBaseJavaModule
public class ReactNativeBitmovinPlayerModule extends ReactContextBaseJavaModule {
...
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
// Hiding the ActionBar
if (isInPictureInPictureMode) {
getSupportActionBar().hide();
} else {
getSupportActionBar().show();
}
playerView.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
}
...
}
Yes, it's possible to achieve it. And there's actually more than one way to listen for PiP mode events from native modules.
Quick way
The easiest way is by making your base java module a
LifecycleStateObserver
and checking changes onActivity.isInPictureInPictureMode()
for every activity state update.Note it's impossible to register the lifecycle observer inside the module's constructor because the activity's still
null
. It needs to be registered on the javascript side.Therefore, call
registerLifecycleEventObserver
at your component's initialization so it can start receiving activity state updates.By the way, I opened a pull request on
react-native-bitmovin-player
implementing this very feature. Please, check it out .Hard way
There's yet another way to listen for PiP changes, but it's more complex and requires a deeper knowledge of both android and RN platforms. However, with it, you get the advantage of accessing
newConfig
on theonPictureInPictureModeChanged
method (if required) and not listening to any of the activity's lifecycle events.Start by embedding your custom native view (whatever it is) into a
Fragment
, then override the fragment'sonPictureInPictureModeChanged
method and finally dispatch an RN event there. Here's how it can be done step by step:ViewGroupManager
to hold the fragment and export functions and props to javascript:CustomViewManager
in a package:This last example was heavily based on RN's documentation. And I can't stress enough how important it is to read it if you go the hard way.
Anyways, I hope this little guide may be of help.
Best regards.