您的 Android TV 應用程式可能會支援多種不同音訊/文字的音軌, 語言和環繞音效設定的方式與網路接收器相同 應用程式。為支援多個音軌和選擇音軌, 請在 Android TV 應用程式中實作下列程式碼:
提供曲目資訊和狀態
適用於頂層項目
MediaInfo
、
就能提供
MediaTracks
。
方法是使用
MediaInfoModifier
:
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() ))
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
敬上
以反映目前選取的曲目:
val mediaStatusModifier: MediaTracksModifier = CastReceiverContext.getInstance() .mediaManager.mediaStatusModifier.getMediaTracksModifer() MediaTracksModifier.setActiveTrackIds(longArrayOf(1))
MediaTracksModifier mediaStatusModifier = CastReceiverContext.getInstance() .getMediaManager().getMediaStatusModifier().getMediaTracksModifer(); MediaTracksModifier.setActiveTrackIds(new long[]{1});
上述步驟可確保傳送者追蹤選項對話方塊反映出 正確狀態。
處理音軌選取作業
您必須先宣告
MediaStatus.COMMAND_EDIT_TRACKS
敬上
做為支援的媒體指令
MediaStatusModifier
:
CastReceiverContext .getInstance() .getMediaManager() .getMediaStatusModifier() .setMediaCommandSupported(MediaStatus.COMMAND_EDIT_TRACKS, true)
CastReceiverContext .getInstance() .getMediaManager() .getMediaStatusModifier() .setMediaCommandSupported(MediaStatus.COMMAND_EDIT_TRACKS, true);
當使用者在傳送方端的追蹤選取對話方塊中選取音軌時,
Android TV 應用程式收到回呼,可變更所選測試群組。
覆寫以處理指令
MediaCommandCallback
:
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 { ... } }
public class MyMediaCommandCallback extends MediaCommandCallback { /** Text selection callback scoped to individual track types. */ @Override public TaskonSelectTracksByType( 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) { ... } }