title: exoplayer - Enable subtitle on video
body: <p>I want to add subtitle on video from srt (or vtt or ttml).</p>
  <p>What I have made till now -
  We have an api by which we get youtube video url with the subtitle file link on our own server. I extract video url from the youtube link with the help of a library and then play that video on exoplayer.</p>
  <p>Till now its working 100%.</p>
  <p>Now we have to add subtitles over the video, for this I download the subtitle file from for the video from our server and save it in internal memory, and while creating the MediaSource for exoplayer we set the subtitle file.</p>
  <p>I have tried almost every thing on the internet but not able to set subtitle on the video. I am sharing my code here with XML and java file, also the code how I am setting the subtitle file.</p>
  <pre><code>activity_video.xml
  
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:background=&quot;#D9000000&quot;
      android:orientation=&quot;vertical&quot;
      android:gravity=&quot;center&quot;
      tools:context=&quot;.Activities.Video_Activity&quot;
      android:keepScreenOn=&quot;true&quot;&gt;
  
      &lt;com.google.android.exoplayer2.ui.PlayerView
          android:id=&quot;@+id/video_view&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;230dp&quot;
          android:layout_below=&quot;@+id/video_navigation_bar&quot;
          android:background=&quot;@android:color/transparent&quot;
          app:controller_layout_id=&quot;@layout/layout_exoplayer_control_views&quot;&gt;
  
          &lt;ProgressBar
              android:id=&quot;@+id/video_buffer_indicator&quot;
              android:layout_width=&quot;48dp&quot;
              android:layout_height=&quot;48dp&quot;
              android:layout_gravity=&quot;center&quot;
              android:translationZ=&quot;@dimen/_51sdp&quot;
              android:visibility=&quot;gone&quot; /&gt;
  
      &lt;/com.google.android.exoplayer2.ui.PlayerView&gt;
  </code></pre>
  <pre><code>MediaItem.SubtitleConfiguration subtitle =
                              new MediaItem.SubtitleConfiguration.Builder(Uri.parse(filePath))
                                      .setMimeType(MimeTypes.APPLICATION_TTML) // The correct MIME type (required).
                                      .setLanguage(&quot;en-US&quot;) // The subtitle language (optional).
                                      .setSelectionFlags(C.SELECTION_FLAG_AUTOSELECT) // Selection flags for the track (optional).
                                      .build();
                      MediaItem mediaItem =
                              new MediaItem.Builder()
                                      .setUri(String.valueOf(mediaSource.get(&quot;video_link&quot;)))
                                      .setSubtitleConfigurations(ImmutableList.of(subtitle))
                                      .build();
                      list.add((new ProgressiveMediaSource.Factory(factory)).createMediaSource(mediaItem));
   exoPlayer.setMediaSources(list);
              exoPlayer.prepare();
              exoPlayer.seekTo(currentlyPlayingIndex, C.TIME_UNSET);
              gc.onBoardingPlayListStatus.get(currentlyPlayingIndex).replace(&quot;watching&quot;, true);
              videoAdapter.notifyItemChanged(currentlyPlayingIndex);
              exoPlayer.setPlayWhenReady(true);
              exoPlayer.play();
              videoPlayer.getSubtitleView().setVisibility(View.VISIBLE);
  
  </code></pre>
  <p>Thanks in advance.</p>