曲目

您的 Android TV 应用可能支持为不同的应用提供多个音频/文字轨道, 语言和环绕声设置与网络接收器相同 。为了支持多个轨道和轨道选择,您需要 在 Android TV 应用中实现以下功能:

提供轨道信息和状态

对于顶级 MediaInfo、 提供可用的 MediaTracks 使用 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()
  ));

使用 MediaTracksModifier 来反映当前选定的曲目:

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

通过上述步骤,可以确保发件人的“跟踪”选择对话框反映的是 正确状态。

处理轨道选择

如需支持选择轨道,您必须先声明 MediaStatus.COMMAND_EDIT_TRACKS 作为受支持的媒体命令 MediaStatusModifier:

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

当用户在发送端的轨道选择对话框中选择轨道时, 您的 Android TV 应用会收到回调以更改所选轨道。 通过替换处理命令 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) {
    ...
  }
}