<activity android:name="VideoActivity" android:resizeableActivity="true" android:supportsPictureInPicture="true" android:configChanges= "screenSize|smallestScreenSize|screenLayout|orientation" ...
处理布局配置的变化,以免画中画模式下布局发生改变时Activity被重建。
getActivity().enterPictureInPictureMode();
调用这行代码主动进入画中画模式。
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
if (isInPictureInPictureMode) {
// Hide the controls in picture-in-picture mode.
...
} else {
// Restore the playback UI based on the playback status.
...
}
}
新增了一个onPictureInPictureModeChanged
,在画中画模式下可以隐藏某些UI。
@Override
public void onPause() {
// If called while in PIP mode, do not pause playback
if (isInPictureInPictureMode()) {
// Continue playback
...
}
// If paused but not in PIP, pause playback if necessary
...
}
Activity转换成画中画模式时,系统把它当做pause状态,会调用onPause方法。某些在画中画模式不能停止的服务,需要继续保持。
画中画模式下,默认得不到任何输入焦点,为了收到输入事件,调用MediaSession.setMediaButtonReceiver()