google / ExoPlayer

This project is deprecated and stale. The latest ExoPlayer code is available in https://github.com/androidx/media
https://developer.android.com/media/media3/exoplayer
Apache License 2.0
21.71k stars 6.02k forks source link

How to show the subtitles at the beginning? #6727

Closed tuxxon closed 4 years ago

tuxxon commented 4 years ago

@kim-vde

Hello.

Once I try to show the subtitles at the beginning, I got some problems in the following code,

My code is

        int textRenderer = Utils.getRendererIndex(simpleExoPlayer, C.TRACK_TYPE_TEXT);
        final TrackGroupArray textTracks = trackSelector.getCurrentMappedTrackInfo()
                .getTrackGroups(textRenderer);

        List<String> availableLanguages = new ArrayList<>(textTracks.length);
        trackSelector.setPreferredTextLanguage(captionLanguage);

In this code, the problem is where getCurrentMappedTrackInfo() is null.

I want to receive events on onCues(List\<Cue> cues) as soon as the player begins. Please let me know how to show the subtitles at the beginning.

kim-vde commented 4 years ago

The mapped track info is only available after the player has been prepared. Even in this case, the information is not directly available and you need to use Player.EventListener.onTracksChanged() to make sure getCurrentMappedTrackInfo() is not null.

Also, the preferred language should ideally be selected before the player is prepared.

Therefore, you should: 1) select the preferred language, 2) call player.prepare(), 3) retrieve the mapped track info though a listener.

If it is not possible because you need the available languages to select the preferred language, you can set the preferred language later but it is less performent as the player may already have loaded the subtitles in another language. You also need to make sure that the player does not start playing in another language before you set the preferred language.

tuxxon commented 4 years ago

I am appreciated for your comment and gonna try this.