2

I'm using the Companion library for casting video from my app to the Chromecast. Is there any way ho I can add the subtitles / closed captions toggle button so the user will be able to turn them on and off?

I'm reading their documentation where I can see, how is possible to set the subtitle 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();

But no words about where I should handle the show / hide actions.

Do you have any suggestions how I can add the toggle button and handle show / hide actions?

I'm using the VideoCastManager which is using the VideoCastControllerActivity from the casting library.

This is my 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);

And there I'm creating the 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();
Stepan Sanda
  • 2,322
  • 7
  • 31
  • 55

1 Answers1

2

You need to do the following:

  1. Make sure your MediaInfo items have tracks info.

  2. Make sure that tracks are enabled in the settings and enable the support for tracks when you configure CastVideoManager.

  3. Register a OnTracksSelectedListener listener in your, say, activity so that when tracks change, your activity can be notified.

4.Add a button to your activity and upon a click on the button, call a method like the following.

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

This will open a (fragment) dialog that shows the current text and audio tracks and allows the user to select one. When one is selected and Ok is pressed in that dialog, the listener that you had registered in the previous step is called and then you can enable the track in your listener.

  1. Make sure you remove the listener when you leave your activity.
Ali Naddaf
  • 16,951
  • 2
  • 21
  • 28
  • Thank you for your answer. But I have custom player for android and I'm using the VideoCastManager which is using the VideoCastControllerActivity. I don't know how I can add the CC button and set OnClickListener on it even I can see, that there is the CC button inside cast_activity.xml layout but it is not visible and I don't know how I can enable it. Thank you for your time – Stepan Sanda Feb 13 '16 at 22:29
  • I edited my question with more information. You were right, I'm using the 'VideoCastControllerActivity' – Stepan Sanda Feb 13 '16 at 22:59
  • If you are using CastControllerActivity and if you have enabled close captions when you initialized the VideoCastManager and if your media has audio or text tracks configured in it, then the button would show up, check the CastVideos-android app – Ali Naddaf Feb 13 '16 at 22:59
  • I edited my question with more info how I'm initialising the the manager. I enabled captions and also set them as text tracks as it is described in the documentation. Even after that, the CC button is still now visible. – Stepan Sanda Feb 13 '16 at 23:12
  • Have you turned on captions in Android settings? – Ali Naddaf Feb 14 '16 at 00:12
  • Thank you for your info! No I didn't. Now when I enable it in the settings the CC button is visible. But I need to make it visible all the time, we have custom CC settings in our app. Is there any way how I can add a custom CC button when I'm using the VideoCastControllerActivity? – Stepan Sanda Feb 14 '16 at 07:50
  • There is no configuration for that; if you want to modify CCL, you can change TracksPreferenceManager.isCaptionEnabled() to always return true. – Ali Naddaf Feb 14 '16 at 08:20
  • But this method isCaptionEnabled() is just returning the boolean value, there is no way how I can set this value to be true. – Stepan Sanda Feb 14 '16 at 14:53
  • Because I'm including the library from jCenter, the only option is to add it as module dependency and change the code of it right? – Stepan Sanda Feb 14 '16 at 15:01
  • Another option is to extend `VideoCastControllerActivity` and only override `setClosedCaptionState`. Meanwhile, you can also open a feature request on CCL's github project to add that as a configuration parameter. – Ali Naddaf Feb 14 '16 at 16:32
  • Is there a simpler way to do this? it seems really goofy to have to rely on Android settings to toggle captions. We'd like to just provide a simple toggle. – Nelson Ramirez Apr 25 '16 at 20:14