When I pressed android home button,play pip video via native modules and pass video url and current duration of video and Native Module accept that parameter and intent to Video Activity.Inside Video Activity OnCreate method,I try to show PiP video.When I pressed on icon in react native,PiP mode is show but I cannot go back to react native view.When I pressed on Android Home Button,PiP mode cannot show.
PiPModule.java
public class PiPModule extends ReactContextBaseJavaModule {
public PiPModule(ReactApplicationContext reactContext){
super(reactContext);
}
@NonNull
@Override
public String getName() {
return "PiPModule";
}
@ReactMethod
public void showPiPVideo(String videoURL,int seekToDuration)
{
Toast.makeText(getReactApplicationContext(),"showPiPVideo is working",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getCurrentActivity(), PiPVideoActivity.class);
intent.putExtra("VIDEO_URL",videoURL);
intent.putExtra("VIDEO_CURRENT_DURATION",seekToDuration);
getCurrentActivity().startActivity(intent);
}
}
Usage in React-Native Component
import {NativeModules} from 'react-native'
const pipVideo = NativeModules.PiPModule;
pipVideo.showPiPVideo(videoURL, Math.floor(currentTime) * 1000);
PiPVideo Activity
public class PiPVideoActivity extends AppCompatActivity {
private String videoPath;
private int videoDuration;
VideoView myVideoView;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pip_video);
Intent i = getIntent();
if(i != null){
myVideoView = (VideoView) findViewById(R.id.videoView);
videoPath = i.getStringExtra("VIDEO_URL");
videoDuration = i.getIntExtra("VIDEO_CURRENT_DURATION",0);
PlayVideo();
}
else{
Toast.makeText(PiPVideoActivity.this, "VideoURL not found", Toast.LENGTH_SHORT).show();
}
PictureInPictureParams params = new PictureInPictureParams
.Builder()
.setAspectRatio(new Rational(1,1))
.build();
enterPictureInPictureMode(params);
}
private void PlayVideo() {
try {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
Uri video = Uri.parse(videoPath);
myVideoView.setVideoURI(video);
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
//seek to receive miliseconds
myVideoView.seekTo(videoDuration);
myVideoView.start();
}
});
} catch (Exception e) {
System.out.println("Video Play Error :" + e.toString());
finish();
}
}
}