Android TV
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การนำทางด้วย D-pad บน Android TV
รีโมตคอนโทรลของ Android TV มีการควบคุม D-pad ที่ส่งคำสั่งซึ่ง
มาถึงเป็นเหตุการณ์สำคัญที่ dispatchKeyEvent(KeyEvent)
ของ Activity
คุณต้องมอบสิทธิ์ต่อไปนี้ให้แก่ PlayerView
Kotlin
override fun dispatchKeyEvent(event: KeyEvent?): Boolean{
return playerView.dispatchKeyEvent(event!!) || super.dispatchKeyEvent(event)
}
Java
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return playerView.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);
}
การขอโฟกัสสำหรับ PlayerView
มีความสำคัญต่อการไปยังส่วนต่างๆ ของตัวควบคุมการเล่น
และการข้ามโฆษณา ลองขอโฟกัสใน onCreate
ของ
Activity
ดังนี้
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ...
playerView.requestFocus()
// ...
}
Java
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
playerView.requestFocus();
// ...
}
หากใช้ Compose ใน Android TV คุณต้องทำให้ AndroidView
focusable และมอบสิทธิ์เหตุการณ์โดยส่งพารามิเตอร์ตัวแก้ไขไปยัง
AndroidView
ตามนั้น
AndroidView(
modifier = modifier
.focusable()
.onKeyEvent { playerView.dispatchKeyEvent(it.nativeKeyEvent) },
factory = { playerView }
)
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-27 UTC
[[["เข้าใจง่าย","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-27 UTC"],[],[],null,["D-pad navigation on Android TV\n\nThe remote control of Android TV has a D-pad control that sends commands that\narrive as key events at `dispatchKeyEvent(KeyEvent)` of your `Activity`. These\nneed to be delegated to the [`PlayerView`](/reference/androidx/media3/ui/PlayerView): \n\nKotlin \n\n```kotlin\noverride fun dispatchKeyEvent(event: KeyEvent?): Boolean{\n return playerView.dispatchKeyEvent(event!!) || super.dispatchKeyEvent(event)\n}\n```\n\nJava \n\n```java\n@Override\npublic boolean dispatchKeyEvent(KeyEvent event) {\n return playerView.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);\n}\n```\n\n\u003cbr /\u003e\n\nRequesting focus for the `PlayerView` is important for navigating playback\ncontrols and skipping ads. Consider requesting the focus in `onCreate` of the\n`Activity`: \n\nKotlin \n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n // ...\n playerView.requestFocus()\n // ...\n}\n```\n\nJava \n\n```java\n@Override\npublic void onCreate(@Nullable Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n // ...\n playerView.requestFocus();\n // ...\n}\n```\n\n\u003cbr /\u003e\n\nIf you are using Compose on Android TV, you need to make the [`AndroidView`](/reference/kotlin/androidx/compose/ui/viewinterop/package-summary#AndroidView(kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function1))\nfocusable and delegate the event by passing the modifier parameter into the\n`AndroidView` accordingly: \n\n AndroidView(\n modifier = modifier\n .focusable()\n .onKeyEvent { playerView.dispatchKeyEvent(it.nativeKeyEvent) },\n factory = { playerView }\n )\n\n\u003cbr /\u003e"]]