Kotlin

Lancement de l'image dans l'image

1. Ajouter un bouton pour entrer dans l'image dans l'image

Cachez le bouton lorsque vous êtes déjà en mode PiP. Le câbler de VideoCallScreen:

if (!isInPipMode) {
    Button(
        onClick = onEnterPictureInPicture,
        modifier = Modifier.align(Alignment.TopStart).padding(8.dp),
    ) {
        Text("Enter Picture-In-Picture")
    }
}

2. Entrer en PiP avec contrôle du rapport d'aspect et du dispositif

Utilisation PictureInPictureParams sur API 26+ et vérifiez que l'appareil prend en charge la fonction PiP :

private fun enterPictureInPicture() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        Toast.makeText(this, "Picture-in-picture is not supported on this device.", Toast.LENGTH_SHORT)
            .show()
        return
    }
    if (!packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
        Toast.makeText(this, "Picture-in-picture is not supported on this device.", Toast.LENGTH_SHORT)
            .show()
        return
    }
    try {
        val params = PictureInPictureParams.Builder()
            .setAspectRatio(Rational(9, 16))
            .build()
        enterPictureInPictureMode(params)
    } catch (e: IllegalStateException) {
        Log.e(TAG, "Failed to enter picture-in-picture mode", e)
        Toast.makeText(this, "Could not enter picture-in-picture mode.", Toast.LENGTH_SHORT).show()
    }
}

Ajuster Rational(width, height) pour correspondre à la forme de votre fenêtre PiP préférée (l'exemple utilise le portrait 9:16).