我已经在项目中实现了Twilio Programmable Video Call。通话连接后,我需要支持“画中画”模式。
我添加了一个具有固定高度和宽度的视图以显示本地视频。我单击一个按钮以在PIP模式下进入活动。固定视图也会在PIP模式下调整大小,但是几秒钟后,固定视图会展开。
清单-
<activity
android:name=".call.TwilioVideoCallActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:excludeFromRecents="true"
/>
输入画中画模式-
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
if (!isInPictureInPictureMode) {
showControllers();
}
this.inPictureInPictureMode = isInPictureInPictureMode;
}
private boolean isPIPSupport() {
PackageManager packageManager = getApplicationContext().getPackageManager();
return packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) && canEnterPiPMode(getApplicationContext());
}
public static boolean canEnterPiPMode(Context context) {
AppOpsManager appOpsManager = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
return (AppOpsManager.MODE_ALLOWED == appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), context.getPackageName()));
}
return false;
}
private void hideControllers() {
footerLyt.setVisibility(View.GONE);
callEndBtn.setVisibility(View.GONE);
minimizeBtn.setVisibility(View.GONE);
timeChronometer.setVisibility(View.GONE);
}
private void showControllers() {
footerLyt.setVisibility(View.VISIBLE);
callEndBtn.setVisibility(View.VISIBLE);
minimizeBtn.setVisibility(View.VISIBLE);
timeChronometer.setVisibility(View.VISIBLE);
}
private void minimize() {
// Hide the controls in picture-in-picture mode.
// Calculate the aspect ratio of the PIP screen.
hideControllers();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Rational aspectRatio = new Rational(
3, 4);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
PictureInPictureParams params = new PictureInPictureParams.Builder()
.setAspectRatio(aspectRatio)
.build();
enterPictureInPictureMode(params);
} else {
enterPictureInPictureMode();
}
}
}
@Override
public void onUserLeaveHint() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && isCallConnected && isPIPSupport()) {
if (!isInPictureInPictureMode()) {
minimize();
}
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainRootLyt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:orientation="vertical">
<com.twilio.video.VideoView
android:id="@+id/thumbnail_video_view"
android:layout_width="@dimen/thumb_video_width"
android:layout_height="@dimen/thumb_video_height"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:visibility="gone"
app:mirror="true"
app:overlaySurface="true" />
<com.twilio.video.VideoView
android:id="@+id/primary_video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<-- other childs are in Linear, Relative and Frame -->
</RelativeLayout>