我正在使用配套库将视频从我的应用投射到 Chromecast。有什么方法可以添加字幕/隐藏式字幕切换按钮,以便用户能够打开和关闭它们?
我正在阅读他们的文档,我可以看到,如何设置字幕URL
MediaTrack englishSubtitle = new MediaTrack.Builder(1 /* ID */,
MediaTrack.TYPE_TEXT)
.setName("English Subtitle")
.setSubtype(MediaTrack.SUBTYPE_SUBTITLE)
.setContentId("https://some-url/caption_en.vtt")
/* language is required for subtitle type but optional otherwise */
.setLanguage("en-US")
.build();
但是没有关于我应该在哪里处理显示/隐藏动作的消息。
您对如何添加切换按钮并处理显示/隐藏操作有什么建议吗?
我正在使用使用转换库中VideoCastControllerActivity
的VideoCastManager
。
这是我的CastConfiguration
// Build a CastConfiguration object and initialize VideoCastManager
CastConfiguration options = new CastConfiguration.Builder(MyAppConfig.CHROMECAST_APP_ID)
.enableAutoReconnect()
.enableCaptionManagement()
.enableDebug()
.enableLockScreen()
.enableNotification()
.enableWifiReconnection()
.setCastControllerImmersive(true)
.setLaunchOptions(false, Locale.getDefault())
.setNextPrevVisibilityPolicy(CastConfiguration.NEXT_PREV_VISIBILITY_POLICY_DISABLED)
.addNotificationAction(CastConfiguration.NOTIFICATION_ACTION_REWIND, false)
.addNotificationAction(CastConfiguration.NOTIFICATION_ACTION_PLAY_PAUSE, true)
.addNotificationAction(CastConfiguration.NOTIFICATION_ACTION_DISCONNECT, true)
.setForwardStep(10)
.build();
// Google Chrome Cast initialization of the VideoCastManager that is a helper class from the CasCompanionLibrary
// that helps us deal with the flow of communicating with chromecast
VideoCastManager.
initialize(this, options)
.setVolumeStep(MyAppConfig.VOLUME_INCREMENT);
我在那里创建MediaInfo
MediaMetadata mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
mediaMetadata.addImage(new WebImage(Uri.parse(MyAppConfigBase.IMAGE_API_ENDPOINT + movieVideoItem.getImages().getKeyart())));
mediaMetadata.addImage(new WebImage(Uri.parse(MyAppConfigBase.IMAGE_API_ENDPOINT + movieVideoItem.getImages().getKeyart())));
mediaMetadata.putString(MediaMetadata.KEY_TITLE, movieVideoItem.getTitle());
mediaMetadata.putString(MediaMetadata.KEY_SUBTITLE, movieVideoItem.getDescription());
mediaMetadata.putString("movie-urls", url);
mediaMetadata.putString("content-type", movieVideoItem.getContent().getHighRes().getType());
MediaTrack englishSubtitle = new MediaTrack.Builder(1 /* ID */, MediaTrack.TYPE_TEXT)
.setName("English Subtitle")
.setSubtype(MediaTrack.SUBTYPE_CAPTIONS)
.setContentId(closedCaptionsUrl)
/* language is required for subtitle type but optional otherwise */
.setLanguage("en-US")
.build();
List tracks = new ArrayList();
tracks.add(englishSubtitle);
MediaInfo mediaInfo = new MediaInfo.Builder(url)
.setStreamDuration(movieVideoItem.getDuration())
.setStreamType(MediaInfo.STREAM_TYPE_NONE)
.setContentType(type)
.setMetadata(mediaMetadata)
.setMediaTracks(tracks)
.setCustomData(customData)
.build();
您需要执行以下操作:
-
确保您的媒体信息项目具有曲目信息。
-
确保在设置中启用了轨道,并在配置CastVideoManager时启用对轨道的支持。
-
在您的活动中注册一个
OnTracksSelectedListener
侦听器,以便在轨道发生变化时,可以通知您的活动。
4.向活动添加一个按钮,单击该按钮后,调用如下所示的方法。
private void showTracksChooserDialog()
throws TransientNetworkDisconnectionException, NoConnectionException {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag(DIALOG_TAG);
if (prev != null) {
transaction.remove(prev);
}
transaction.addToBackStack(null);
// Create and show the dialog.
TracksChooserDialog dialogFragment = TracksChooserDialog
.newInstance(mCastManager.getRemoteMediaInformation());
dialogFragment.show(transaction, DIALOG_TAG);
}
这将打开一个(片段)对话框,显示当前文本和音轨,并允许用户选择一个。选择一个并在该对话框中按 Ok 时,将调用您在上一步中注册的侦听器,然后您可以在侦听器中启用该轨道。
- 请务必在退出活动记录时移除侦听器。