0

I am developing an Android app for TV using the Leanback library. I have an HLS video stream with an srt subtitle from a URI. I am using ExoPlayer version 2.5.4 as used in this example app. I created my MediaSource using:

private MediaSource onCreateMediaSource(Uri uri, Uri subtitleUri) {
    String userAgent = Util.getUserAgent(mContext, "ExoPlayerAdapter");
    MediaSource videoSource = new HlsMediaSource(uri,
            new DefaultDataSourceFactory(mContext, userAgent),
            null,
            null);
    Format subtitleFormat = Format.createTextSampleFormat(
            "1", MimeTypes.APPLICATION_SUBRIP, 0, "en");
    MediaSource subtitleSource = new SingleSampleMediaSource(
            subtitleUri,
            new DefaultDataSourceFactory(mContext, userAgent),
            subtitleFormat, C.TIME_UNSET);

    MergingMediaSource mergedSource =
            new MergingMediaSource(videoSource, subtitleSource);
    return mergedSource;
}

In my PlaybackTransportControlGlue, I have a PlaybackControlsRow.ClosedCaptioningAction. When this button is clicked, what do I write in the action dispatcher to toggle the closed captions?

I tried this:

@Override
public void onActionClicked(Action action) {
    if (action == mClosedCaptioningAction) {
        DefaultTrackSelector trackSelector = mAdapter.getTrackSelector();
        int rendererIndex = 0;
        if (mClosedCaptioningAction.getIndex() == PlaybackControlsRow.ClosedCaptioningAction.INDEX_ON) {
            MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
            TrackGroupArray textGroups = mappedTrackInfo.getTrackGroups(rendererIndex);
            int groupIndex = 0;

            trackSelector.setRendererDisabled(rendererIndex, false);
            MappingTrackSelector.SelectionOverride override =
                    new MappingTrackSelector.SelectionOverride(mTrackFactory, groupIndex, 0);
            trackSelector.setSelectionOverride(rendererIndex, textGroups, override);
        } else {
            trackSelector.setRendererDisabled(rendererIndex, true);
            trackSelector.clearSelectionOverrides();
        }
    }
    super.onActionClicked(action);
}

I got this error:

E/gralloc: unregister FBM buffer
khateeb
  • 5,265
  • 15
  • 58
  • 114
  • I have an answer that talks about video track selection, but the same logic applies to subtitle track selection. If you need more info, I can write out a more in depth answer here. Just let me know :) https://stackoverflow.com/questions/45835562/quality-selector-for-exoplayer-2/45844705#45844705 There is also this answer which might be more appropriate. Just let me know if it's not what your'e looking for: https://stackoverflow.com/questions/46521570/how-to-add-multiple-subtitles-in-exo-player-2/46547904#46547904 – Kyle Venn Nov 02 '17 at 14:39
  • @KyleVenn I already did your suggestion in the second link. The first link seems to be talking about changing video qualities and audios. i don't want that. I just want to toggle the closed captions on and off when I click the button. I already have a listener on the button. I want to know what code will toggle the CC. – khateeb Nov 14 '17 at 14:15
  • The first link is about video quality (TRACK_TYPE_VIDEO), but the same code should work for closed captions (since they're also backed by track groups). `selector.setSelectionOverride(rendererIndex, trackGroups, override);` will still work for what you want. Instead of `C.TRACK_TYPE_VIDEO` you'd want `C.TRACK_TYPE_TEXT`. If that isn't clear, just let me know and I'll write out a more thorough answer below. – Kyle Venn Nov 14 '17 at 20:47
  • @KyleVenn I tried out what was written in the first link and the ExoPlayer sample app but was unsuccessful. I just cannot wrap my head around this. I have updated the question with my code. I am using the ExoPlayer like this project: https://github.com/googlesamples/leanback-showcase/blob/master/app/src/main/java/android/support/v17/leanback/supportleanbackshowcase/app/media/VideoConsumptionExampleWithExoPlayerFragment.java . In this project, there is a class called `MediaPlayerGlue` where I have written the above code. – khateeb Nov 16 '17 at 09:24
  • Okay I'll take a crack at trying to make their example more clear. I'd actually recommend using [the ExoPlayer demo app](https://github.com/google/ExoPlayer/blob/release-v2/demo/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java) which does exactly what you'd like (allows for subtitle track selection). Running that app (on a phone) and stepping through the code of selecting a subtitle should make it way more clear. It'll work just the same on TV. – Kyle Venn Nov 16 '17 at 15:10
  • @KyleVenn Could you please show the steps how to do it? – khateeb Nov 24 '17 at 12:29

1 Answers1

1

Okay I just answered a question which got text tracks working in a simple way here. This works for adaptive files (like HLS), but I would have to modify it to get it working with progressive files (like an .mp4 that you've merged with an .srt file).

It's at least a starting point.

I'd like to circle back around and get it fully working for you, but I think it may be a matter of working with the track index and tweaking the override so that it doesn't use the AdaptiveFactory (from the below line).

TrackSelection.Factory factory = tracks.length == 1
            ? new FixedTrackSelection.Factory()
            : new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);

We have it fully working in our code for both HLS and progressive, but our implementation is wrapped in a lot of additional architecture which might make it even harder to understand the core components.

Kyle Venn
  • 4,597
  • 27
  • 41