เข้าสู่โหมด PIP ในเวลาที่เหมาะสม

แอปไม่ควรเข้าสู่โหมด PiP ในสถานการณ์ต่อไปนี้

  • หากวิดีโอหยุดหรือหยุดชั่วคราว
  • หากคุณอยู่ในหน้าอื่นของแอปที่ไม่ใช่หน้าวิดีโอเพลเยอร์

หากต้องการควบคุมเวลาที่แอปเข้าสู่โหมด PIP ให้เพิ่มตัวแปรที่ติดตามสถานะของวิดีโอเพลเยอร์โดยใช้ mutableStateOf

สลับสถานะตามว่าวิดีโอเล่นอยู่หรือไม่

หากต้องการสลับสถานะตามว่าวิดีโอเพลเยอร์เล่นอยู่หรือไม่ ให้เพิ่ม Listener ในวิดีโอเพลเยอร์ สลับสถานะของตัวแปรสถานะตามว่าเพลเยอร์เล่นอยู่หรือไม่

player.addListener(object : Player.Listener {
    override fun onIsPlayingChanged(isPlaying: Boolean) {
        shouldEnterPipMode = isPlaying
    }
})

สลับสถานะตามที่มีการเปิดใช้โปรแกรมเล่นหรือไม่

เมื่อเผยแพร่วิดีโอ ให้ตั้งค่าตัวแปรสถานะเป็น false

fun releasePlayer() {
    shouldEnterPipMode = false
}

ใช้สถานะเพื่อระบุว่ามีการเข้าสู่โหมด PiP หรือไม่ (ก่อน Android 12)

  1. เนื่องจากการเพิ่ม PiP ก่อนเวอร์ชัน 12 ใช้ DisposableEffect คุณจึงต้องสร้างตัวแปรใหม่โดย rememberUpdatedState โดยตั้งค่า newValue เป็นตัวแปรสถานะ วิธีนี้จะช่วยให้มั่นใจได้ว่าจะใช้เวอร์ชันที่อัปเดตแล้วภายใน DisposableEffect
  2. ใน Lambda ที่กําหนดลักษณะการทํางานเมื่อมีการเรียกใช้ OnUserLeaveHintListener ให้เพิ่มคำสั่ง if ที่มีตัวแปรสถานะรอบการเรียกใช้ enterPictureInPictureMode() ดังนี้

    val currentShouldEnterPipMode by rememberUpdatedState(newValue = shouldEnterPipMode)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
        Build.VERSION.SDK_INT < Build.VERSION_CODES.S
    ) {
        val context = LocalContext.current
        DisposableEffect(context) {
            val onUserLeaveBehavior: () -> Unit = {
                if (currentShouldEnterPipMode) {
                    context.findActivity()
                        .enterPictureInPictureMode(PictureInPictureParams.Builder().build())
                }
            }
            context.findActivity().addOnUserLeaveHintListener(
                onUserLeaveBehavior
            )
            onDispose {
                context.findActivity().removeOnUserLeaveHintListener(
                    onUserLeaveBehavior
                )
            }
        }
    } else {
        Log.i("PiP info", "API does not support PiP")
    }

ใช้สถานะเพื่อระบุว่ามีการเข้าสู่โหมด PiP หรือไม่ (หลัง Android 12)

ส่งตัวแปรสถานะไปยัง setAutoEnterEnabled เพื่อให้แอปเข้าสู่โหมด PiP ในเวลาที่เหมาะสมเท่านั้น โดยทำดังนี้

val pipModifier = modifier.onGloballyPositioned { layoutCoordinates ->
    val builder = PictureInPictureParams.Builder()

    // Add autoEnterEnabled for versions S and up
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        builder.setAutoEnterEnabled(shouldEnterPipMode)
    }
    context.findActivity().setPictureInPictureParams(builder.build())
}

VideoPlayer(pipModifier)

ใช้ setSourceRectHint เพื่อใช้ภาพเคลื่อนไหวที่ราบรื่น

setSourceRectHint API สร้างภาพเคลื่อนไหวที่ราบรื่นขึ้นสำหรับการเข้าสู่โหมด PIP ใน Android 12 ขึ้นไป ฟีเจอร์นี้จะสร้างภาพเคลื่อนไหวที่ราบรื่นยิ่งขึ้นสำหรับการออกจากโหมด PiP ด้วย เพิ่ม API นี้ลงในเครื่องมือสร้าง PiP เพื่อระบุพื้นที่ของกิจกรรมที่มองเห็นได้หลังจากเปลี่ยนเป็น PiP

  1. เพิ่ม setSourceRectHint() ลงใน builder เฉพาะในกรณีที่สถานะกำหนดว่าแอปควรเข้าสู่โหมด PiP วิธีนี้จะช่วยหลีกเลี่ยงการคำนวณ sourceRect เมื่อแอปไม่จำเป็นต้องเข้าสู่โหมด PIP
  2. หากต้องการตั้งค่า sourceRect ให้ใช้ layoutCoordinates ที่ได้จากฟังก์ชัน onGloballyPositioned ในตัวแก้ไข
  3. เรียกใช้ setSourceRectHint() ใน builder และส่งตัวแปร sourceRect

val context = LocalContext.current

val pipModifier = modifier.onGloballyPositioned { layoutCoordinates ->
    val builder = PictureInPictureParams.Builder()
    if (shouldEnterPipMode) {
        val sourceRect = layoutCoordinates.boundsInWindow().toAndroidRectF().toRect()
        builder.setSourceRectHint(sourceRect)
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        builder.setAutoEnterEnabled(shouldEnterPipMode)
    }
    context.findActivity().setPictureInPictureParams(builder.build())
}

VideoPlayer(pipModifier)