【问题标题】:Android Chromecast Companion Library - subtitle toggle buttonAndroid Chromecast Companion Library - 字幕切换按钮
【发布时间】:2016-05-22 08:06:25
【问题描述】:

我正在使用 Companion library 将视频从我的应用程序投射到 Chromecast。 有什么办法可以添加字幕/隐藏式字幕切换按钮,以便用户能够打开和关闭它们?

我正在阅读他们的documentation,在那里我可以看到,如何设置字幕网址

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

但是没有关于我应该在哪里处理显示/隐藏操作的消息。

您对如何添加切换按钮和处理显示/隐藏操作有什么建议吗?

我正在使用 VideoCastManager,它正在使用来自铸造库的 VideoCastControllerActivity

这是我的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();

【问题讨论】:

  • 你没有使用 VideoCastControllerActivity 吗?
  • 不,我有自定义活动,我正在播放视频,我还可以将视频流式传输到 Chromecast。我正在使用示例项目中的 ChromeCastUtility。
  • @AliNaddaf 我明白了,你是对的!我正在使用 VideoCastControllerActivity!对不起,误会了,我的错。

标签: android streaming video-streaming chromecast castcompanionlibrary


【解决方案1】:

您需要执行以下操作:

  1. 确保您的 MediaInfo 项目具有曲目信息。

  2. 确保在设置中启用轨道,并在配置 CastVideoManager 时启用对轨道的支持。

  3. 在您的活动中注册一个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);
}

这将打开一个(片段)对话框,显示当前的文本和音轨,并允许用户选择一个。选择一个人并在该对话框中按下一个问题时,调用您在上一步中注册的侦听器,然后您可以在侦听器中启用曲目。

  1. 确保在离开活动时移除监听器。

【讨论】:

  • 感谢您的回答。但是我有适用于 android 的自定义播放器,并且我使用的是 VideoCastManager,它正在使用 VideoCastControllerActivity。即使我可以看到,我也不知道如何添加 CC 按钮并在其上设置 OnClickListener,cast_activity.xml 布局中有 CC 按钮,但它不可见,我不知道如何启用它。感谢您的宝贵时间
  • 我用更多信息编辑了我的问题。你是对的,我正在使用“VideoCastControllerActivity”
  • 如果您正在使用 CastControllerActivity 并且在初始化 VideoCastManager 时启用了隐藏字幕,并且如果您的媒体中配置了音频或文本轨道,则该按钮会显示,请检查 CastVideos-android应用
  • 我编辑了我的问题,提供了更多关于我如何初始化管理器的信息。我启用了字幕并将它们设置为文本轨道,如文档中所述。即使在那之后,CC 按钮现在仍然可见。
  • 您是否在 Android 设置中打开了字幕?
猜你喜欢
  • 1970-01-01
  • 2015-04-15
  • 2010-10-08
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多