I want to add subtitle on video from srt (or vtt or ttml).
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.
Till now its working 100%.
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.
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.
activity_video.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#D9000000"
android:orientation="vertical"
android:gravity="center"
tools:context=".Activities.Video_Activity"
android:keepScreenOn="true">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_below="@+id/video_navigation_bar"
android:background="@android:color/transparent"
app:controller_layout_id="@layout/layout_exoplayer_control_views">
<ProgressBar
android:id="@+id/video_buffer_indicator"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:translationZ="@dimen/_51sdp"
android:visibility="gone" />
</com.google.android.exoplayer2.ui.PlayerView>
MediaItem.SubtitleConfiguration subtitle =
new MediaItem.SubtitleConfiguration.Builder(Uri.parse(filePath))
.setMimeType(MimeTypes.APPLICATION_TTML) // The correct MIME type (required).
.setLanguage("en-US") // 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("video_link")))
.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("watching", true);
videoAdapter.notifyItemChanged(currentlyPlayingIndex);
exoPlayer.setPlayWhenReady(true);
exoPlayer.play();
videoPlayer.getSubtitleView().setVisibility(View.VISIBLE);
Thanks in advance.