3

I am using Exoplayer to play different types of videos, I have download this demo and work with it

https://github.com/google/ExoPlayer

now I need to add subtitle for my video, I am using ExoPlayer version 2.9.5

this is the method that build the media source

private MediaSource buildMediaSource(Uri uri, @Nullable String overrideExtension) {
    @ContentType int type = Util.inferContentType(uri, overrideExtension);
    switch (type) {
      case C.TYPE_DASH:
        return new DashMediaSource.Factory(dataSourceFactory)
            .setManifestParser(
                new FilteringManifestParser<>(new DashManifestParser(), getOfflineStreamKeys(uri)))
            .createMediaSource(uri);
      case C.TYPE_SS:
        return new SsMediaSource.Factory(dataSourceFactory)
            .setManifestParser(
                new FilteringManifestParser<>(new SsManifestParser(), getOfflineStreamKeys(uri)))
            .createMediaSource(uri);
      case C.TYPE_HLS:
        return new HlsMediaSource.Factory(dataSourceFactory)
            .setPlaylistParserFactory(
                new DefaultHlsPlaylistParserFactory(getOfflineStreamKeys(uri)))
            .createMediaSource(uri);
      case C.TYPE_OTHER:
        return new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
      default: {
        throw new IllegalStateException("Unsupported type: " + type);
      }
    }
  }

I guess this is the place that should be changed to add subtitle to my media file, But I do not know how ?

also the subtitle will be from server and with .str format

can anyone help please ?

here is my updated code

private MediaSource buildMediaSourceWithSubtitle (Uri uri, @Nullable String overrideExtension, Uri subtitle) {

    Format subtitleFormat = Format.createTextSampleFormat(
            null, // An identifier for the track. May be null.
            MimeTypes.APPLICATION_SUBRIP, // The mime type. Must be set correctly.
            0, // Selection flags for the track.
            null); // The subtitle language. May be null.
    MediaSource subtitleSource =
            new SingleSampleMediaSource.Factory(dataSourceFactory)
        .createMediaSource(subtitle, subtitleFormat, C.TIME_UNSET);

    MediaSource mediaSource = null;

    @ContentType int type = Util.inferContentType(uri, overrideExtension);
    switch (type) {
      case C.TYPE_DASH:
        mediaSource = new DashMediaSource.Factory(dataSourceFactory)
                .setManifestParser(
                        new FilteringManifestParser<>(new DashManifestParser(), getOfflineStreamKeys(uri)))
                .createMediaSource(uri);

        return new MergingMediaSource(mediaSource, subtitleSource);

      case C.TYPE_SS:
        mediaSource = new SsMediaSource.Factory(dataSourceFactory)
                .setManifestParser(
                        new FilteringManifestParser<>(new SsManifestParser(), getOfflineStreamKeys(uri)))
                .createMediaSource(uri);
        return new MergingMediaSource(mediaSource, subtitleSource);

      case C.TYPE_HLS:
        mediaSource = new HlsMediaSource.Factory(dataSourceFactory)
                .setPlaylistParserFactory(
                        new DefaultHlsPlaylistParserFactory(getOfflineStreamKeys(uri)))
                .createMediaSource(uri);
        return new MergingMediaSource(mediaSource, subtitleSource);

      case C.TYPE_OTHER:
        mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
        return new MergingMediaSource(mediaSource, subtitleSource);
      default: {
        throw new IllegalStateException("Unsupported type: " + type);
      }
    }
  }

enter image description here

Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

3 Answers3

1

It really depends on if you are side loading the subtitles or if they are embedded in your stream. If they are embedded the HLS, Dash, or Other mediasources will detect the tracks and you will need to select the text track. See SimpleExoPlayer example

If you are side loading you need to use SingleSampleMediaSource and a MergingMediaSource. First get your video/audio sources then create a SingleSampleMediaSource from the srt and merge the two using the MergingMediaSource Example. Once that is done select the text track like above.

Ge3ng
  • 1,875
  • 1
  • 24
  • 38
1
     public MediaSource getTextSource(Uri subTitle) {     //subTitle = Uri.parse(file.getAbsolutePath().toString());  file -- the local .srt file
    //todo C.SELECTION_FLAG_AUTOSELECT language MimeTypes
    Format textFormat = new Format.Builder()
            .setSampleMimeType(MimeTypes.APPLICATION_SUBRIP)
            .setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
            .build();

    MediaItem.SubtitleConfiguration  subtitle = new MediaItem.SubtitleConfiguration.Builder(subTitle)
            .setMimeType(checkNotNull(textFormat.sampleMimeType))
            .setLanguage( textFormat.language)
            .setSelectionFlags(textFormat.selectionFlags).build();

    DefaultHttpDataSource.Factory  factory = new DefaultHttpDataSource.Factory()
            .setAllowCrossProtocolRedirects(true)
            .setConnectTimeoutMs(50000)
            .setReadTimeoutMs(50000)
            .setTransferListener( new DefaultBandwidthMeter.Builder(this).build());

    MediaSource textMediaSource = new SingleSampleMediaSource.Factory(new DefaultDataSource.Factory(this,
            factory))
            .createMediaSource(subtitle, C.TIME_UNSET);
    return textMediaSource;
}

then in initplayer()

concatenatingMediaSource = new ConcatenatingMediaSource();
 MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(videoUri));
MediaSource subtitleSource = getTextSource(subtitleUri);
MergingMediaSource mergingMediaSource = new MergingMediaSource(mediaSource, subtitleSource);
concatenatingMediaSource.addMediaSource(mergingMediaSource);
player.setMediaSource(concatenatingMediaSource);
b y f hc
  • 31
  • 2
0

Use below code to enable subtitle on Exoplayer

private void playWithCaption() {

        MediaItem.SubtitleConfiguration subtitle =
                new MediaItem.SubtitleConfiguration.Builder(subtitleUri)
                        .setMimeType(MimeTypes.APPLICATION_SUBRIP) // The correct MIME type (required).
                        .setLanguage("en") // MUST, The subtitle language (optional).
                        .setSelectionFlags(C.SELECTION_FLAG_DEFAULT) //MUST,  Selection flags for the track (optional).
                        .build();
        MediaItem mediaItem =
                new MediaItem.Builder()
                        .setUri(videoUri)
                        .setSubtitleConfigurations(ImmutableList.of(subtitle))
                        .build();

        player.setMediaItem(mediaItem);

        player.prepare();;


        player.setPlayWhenReady(true);

    }

use below Dependancy

implementation "com.google.android.exoplayer:exoplayer:2.17.1"
implementation "com.google.android.exoplayer:exoplayer-core:2.17.1"
implementation "com.google.android.exoplayer:exoplayer-ui:2.17.1"
Vinesh Chauhan
  • 1,288
  • 11
  • 27