เข้าสู่โหมด 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 = Runnable {
                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)