關於子母畫面 (PiP)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
子母畫面 (PiP) 是一種特殊的多視窗模式,主要用於影片播放。這個模式可讓使用者透過固定在畫面角落的小視窗觀看影片,同時繼續在主要畫面使用應用程式或瀏覽內容。
子母畫面會利用 Android 7.0 的多視窗 API 提供固定的影片重疊視窗。如要為應用程式新增子母畫面功能,您必須登錄活動、視需要將活動切換至子母畫面模式,並確認活動處於子母畫面模式時,UI 元素皆為隱藏狀態且影片會繼續播放。
在子母畫面模式下處理 UI
進入子母畫面模式時,除非您指定 UI 在子母畫面模式中和離開子母畫面模式時的顯示方式,否則應用程式的整個 UI 都會進入子母畫面視窗。
首先,您必須知道應用程式是否處於子母畫面模式。您可以使用 OnPictureInPictureModeChangedProvider
達成此目標。下方程式碼會指出應用程式是否處於子母畫面模式。
@Composable
fun rememberIsInPipMode(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val activity = LocalContext.current.findActivity()
var pipMode by remember { mutableStateOf(activity.isInPictureInPictureMode) }
DisposableEffect(activity) {
val observer = Consumer<PictureInPictureModeChangedInfo> { info ->
pipMode = info.isInPictureInPictureMode
}
activity.addOnPictureInPictureModeChangedListener(
observer
)
onDispose { activity.removeOnPictureInPictureModeChangedListener(observer) }
}
return pipMode
} else {
return false
}
}
您現在可以使用 rememberIsInPipMode()
切換應用程式進入子母畫面模式時要顯示的 UI 元素:
val inPipMode = rememberIsInPipMode()
Column(modifier = modifier) {
// This text will only show up when the app is not in PiP mode
if (!inPipMode) {
Text(
text = "Picture in Picture",
)
}
VideoPlayer()
}
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-23 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-23 (世界標準時間)。"],[],[],null,["# About picture-in-picture (PiP)\n\nPicture-in-picture (PiP) is a special type of multi-window mode mostly used for\nvideo playback. It lets the user watch a video in a small window pinned to a\ncorner of the screen while navigating between apps or browsing content on the\nmain screen.\n\nPiP leverages the multi-window APIs made available in Android 7.0 to provide the\npinned video overlay window. To add PiP to your app, you need to register your\nactivity, switch your activity to PiP mode as needed, and make sure UI elements\nare hidden and video playback continues when the activity is in PiP mode.\n\nHandle your UI in PiP mode\n--------------------------\n\nWhen you enter PiP mode, your app's entire UI enters the PiP window unless you\nspecify how your UI should look in and out of PiP mode.\n\nFirst, you need to know when your app is in PiP mode or not. You can use\n[`OnPictureInPictureModeChangedProvider`](/reference/androidx/core/app/OnPictureInPictureModeChangedProvider) to achieve this.\nThe code below tells you if your app is in PiP mode.\n\n\n```kotlin\n@Composable\nfun rememberIsInPipMode(): Boolean {\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.O) {\n val activity = LocalContext.current.findActivity()\n var pipMode by remember { mutableStateOf(activity.isInPictureInPictureMode) }\n DisposableEffect(activity) {\n val observer = Consumer\u003cPictureInPictureModeChangedInfo\u003e { info -\u003e\n pipMode = info.isInPictureInPictureMode\n }\n activity.addOnPictureInPictureModeChangedListener(\n observer\n )\n onDispose { activity.removeOnPictureInPictureModeChangedListener(observer) }\n }\n return pipMode\n } else {\n return false\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt#L119-L137\n```\n\n\u003cbr /\u003e\n\nNow, you can use `rememberIsInPipMode()` to toggle which UI elements to show\nwhen the app enters PiP mode:\n\n\n```kotlin\nval inPipMode = rememberIsInPipMode()\n\nColumn(modifier = modifier) {\n // This text will only show up when the app is not in PiP mode\n if (!inPipMode) {\n Text(\n text = \"Picture in Picture\",\n )\n }\n VideoPlayer()\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt#L152-L162\n```\n\n\u003cbr /\u003e"]]