Tracks

Your Android TV app may support multiple audio/text tracks for different languages and surround sound settings in the same way as web receiver apps. In order to support multiple tracks and track selection, you need to implement the following in your Android TV app:

Provide track info and status

For the top-level MediaInfo, provide the available MediaTracks using the MediaInfoModifier:

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()
  ));

Use MediaTracksModifier to reflect the currently selected tracks:

MediaTracksModifier mediaStatusModifier =
  CastReceiverContext.getInstance().getMediaManager().getMediaStatusModifier().getMediaTracksModifer();

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

The above steps can make sure the sender track selection dialog reflects the correct state.

Handle track selection

To support selecting a track, you must first declare MediaStatus.COMMAND_EDIT_TRACKS as a supported media command in MediaStatusModifier:

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

When the user selects tracks in the track selection dialog on the sender side, your Android TV app receives a callback to change the selected tracks. Handle the command by overriding MediaCommandCallback:

public class MyMediaCommandCallback extends MediaCommandCallback {
  /** Text selection callback scoped to individual track types. */
  @Override
  public Task<Void> onSelectTracksByType(
    String senderId, int type, List<MediaTrack> 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<Void> 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<Void> 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<Void> onEditAudioTracks(
    String senderId, EditAudioTracksData editAudioTracksData) {
    ...
  }
}