Pistas

Tu app de Android TV puede admitir varias pistas de audio o texto para diferentes idiomas y configuración de sonido envolvente del mismo modo que con el receptor web de Google Chat. Para admitir varias pistas y la selección de pistas, debes hacer lo siguiente: implementa lo siguiente en tu app para Android TV:

Proporciona la información y el estado de la pista

Para el nivel superior, MediaInfo: proporcionan los MediaTracks con el MediaInfoModifier:

Kotlin
val mediaInfoModifier = CastReceiverContext.getInstance()
            .mediaManager.mediaStatusModifier.mediaInfoModifier

mediaInfoModifier.setMediaTracks(Arrays.asList(
    new MediaTrack.Builder(1, MediaTrack.TYPE_AUDIO)
        .setName("English")
        ...
        build(),
    new MediaTrack.Builder(2, MediaTrack.TYPE_AUDIO)
        .setName("Spanish")
        ...
        .build()
  ))
.
.
Java
MediaInfoModifier mediaInfoModifier = CastReceiverContext.getInstance()
    .getMediaManager().getMediaStatusModifier().getMediaInfoModifier();

mediaInfoModifier.setMediaTracks(Arrays.asList(
    new MediaTrack.Builder(1, MediaTrack.TYPE_AUDIO)
        .setName("English")
        ...
        build(),
    new MediaTrack.Builder(2, MediaTrack.TYPE_AUDIO)
        .setName("Spanish")
        ...
        .build()
  ));

Usa MediaTracksModifier para reflejar las pistas seleccionadas actualmente:

Kotlin
val mediaStatusModifier: MediaTracksModifier =
      CastReceiverContext.getInstance()
          .mediaManager.mediaStatusModifier.getMediaTracksModifer()

MediaTracksModifier.setActiveTrackIds(longArrayOf(1))
.
.
Java
MediaTracksModifier mediaStatusModifier =
  CastReceiverContext.getInstance()
    .getMediaManager().getMediaStatusModifier().getMediaTracksModifer();

MediaTracksModifier.setActiveTrackIds(new long[]{1});

Los pasos anteriores permiten garantizar que el diálogo de selección de la pista del remitente refleje el con el estado correcto.

Cómo controlar la selección de pistas

Para admitir la selección de una pista, primero debes declarar MediaStatus.COMMAND_EDIT_TRACKS como un comando multimedia compatible MediaStatusModifier:

Kotlin
CastReceiverContext
    .getInstance()
    .getMediaManager()
    .getMediaStatusModifier()
    .setMediaCommandSupported(MediaStatus.COMMAND_EDIT_TRACKS, true)
.
.
Java
CastReceiverContext
    .getInstance()
    .getMediaManager()
    .getMediaStatusModifier()
    .setMediaCommandSupported(MediaStatus.COMMAND_EDIT_TRACKS, true);

Cuando el usuario selecciona pistas en el cuadro de diálogo de selección de pistas del lado del remitente, tu app de Android TV recibirá una devolución de llamada para cambiar los segmentos seleccionados. Para controlar el comando, anula MediaCommandCallback:

Kotlin
class MyMediaCommandCallback : MediaCommandCallback() {
  /** Text selection callback scoped to individual track types.  */
  override fun onSelectTracksByType(
        senderId: String?, type: Int, tracks: List
    ): Task {
    return Tasks.call {
        // Update the track selection in your app.
        if (type == MediaTrack.TYPE_TEXT) {
             mySelectTextTracks(tracks)
        } else if (type == MediaTrack.TYPE_AUDIO) {
             mySelectAudioTracks(tracks)
        }
        // Update the track selection in the modifier to be used in MediaStatus.
        // This is also scoped to the given track type.
        mediaStatusModifier.getMediaTracksModifier().setActiveTracksByType(
            type, tracks
        )
        null
    }
  }

  /** Callback for setting the text track style.  */
  override fun onSetTextTrackStyle(
        senderId: String?, textTrackStyle: TextTrackStyle
    ): Task {
    return Tasks.call {
        // Update the track style in your app.
        mySetTextTrackStyle(textTrackStyle)
        // Update the track style in the modifier.
        mediaStatusModifier.setTextTrackStyle(textTrackStyle)
        null
    }
  }

  // Override the following callback in case you want to handle the original
  // request. This is strongly not recommended.
  //
  // The default implementation automatically translates into
  // onSelectTracksByType() and onSetTextTrackStyle().
  override fun onEditTracksInfo(
    senderId: String?, editTracksInfoData: EditTracksInfoData
    ): Task {
       ...
  }

  // Override the following callback in case you want to handle the original
  // request. This is strongly not recommended.
  override fun onEditAudioTracks(
      senderId: String?, editAudioTracksData: EditAudioTracksData
    ): Task {
        ...
  }
}
.
.
Java
public class MyMediaCommandCallback extends MediaCommandCallback {
  /** Text selection callback scoped to individual track types. */
  @Override
  public Task onSelectTracksByType(
    String senderId, int type, List tracks) {
    return Tasks.call(() -> {
      // Update the track selection in your app.
      if (type == MediaTrack.TYPE_TEXT) {
        mySelectTextTracks(tracks);
      } else if (type == MediaTrack.TYPE_AUDIO) {
        mySelectAudioTracks(tracks);
      }
      // Update the track selection in the modifier to be used in MediaStatus.
      // This is also scoped to the given track type.
      mediaStatusModifier.getMediaTracksModifier().setActiveTracksByType(
        type, tracks);
      return null;
    });
  }

  /** Callback for setting the text track style. */
  @Override
  public Task onSetTextTrackStyle(
    String senderId, TextTrackStyle textTrackStyle) {
    return Tasks.call(() -> {
      // Update the track style in your app.
      mySetTextTrackStyle(textTrackStyle);
      // Update the track style in the modifier.
      mediaStatusModifier.setTextTrackStyle(textTrackStyle);
      return null;
    });
  }

  // Override the following callback in case you want to handle the original
  // request. This is strongly not recommended.
  //
  // The default implementation automatically translates into
  // onSelectTracksByType() and onSetTextTrackStyle().
  @Override
  public Task onEditTracksInfo(
    String senderId, EditTracksInfoData editTracksInfoData) {
    ...
  }

  // Override the following callback in case you want to handle the original
  // request. This is strongly not recommended.
  @Override
  public Task onEditAudioTracks(
    String senderId, EditAudioTracksData editAudioTracksData) {
    ...
  }
}