Android TV
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Navigation avec le pavé directionnel sur Android TV
La télécommande d'Android TV dispose d'un pavé directionnel qui envoie des commandes qui arrivent sous forme d'événements de touche à dispatchKeyEvent(KeyEvent)
de votre Activity
. Ces droits doivent être délégués à 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);
}
Il est important de demander le focus pour le PlayerView
afin de parcourir les commandes de lecture et de passer les annonces. Envisagez de demander la sélection dans onCreate
de 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();
// ...
}
Si vous utilisez Compose sur Android TV, vous devez rendre AndroidView
focusable et déléguer l'événement en transmettant le paramètre du modificateur dans AndroidView
en conséquence :
AndroidView(
modifier = modifier
.focusable()
.onKeyEvent { playerView.dispatchKeyEvent(it.nativeKeyEvent) },
factory = { playerView }
)
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/03 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/03 (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"]]