I have an app that plays videos using ExoPlayer but I wanted to add an option to pick and load any subtitle file into the player.
This is my code for subtitle:
public void setSubSelectedSubtitle(MediaSource mediaSource, String subtitle, Context context) {
MergingMediaSource mergedSource;
if (subtitle != null) {
Uri subtitleUri = Uri.parse(subtitle);
Format subtitleFormat = Format.createTextSampleFormat(
null, // An identifier for the track. May be null.
MimeTypes.TEXT_VTT, // The mime type. Must be set correctly.
Format.NO_VALUE, // Selection flags for the track.
"en"); // The subtitle language. May be null.
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(context,
Util.getUserAgent(context, getResources().getString(R.string.app_name)), new DefaultBandwidthMeter());
MediaSource subtitleSource = new SingleSampleMediaSource
.Factory(dataSourceFactory)
.createMediaSource(subtitleUri, subtitleFormat, C.TIME_UNSET);
mergedSource = new MergingMediaSource(mediaSource, subtitleSource);
player.prepare(mergedSource, false, false);
//resumePlayer();
} else {
Toast.makeText(context, "there is no subtitle", Toast.LENGTH_SHORT).show();
}
}
What I'm trying is, when the user clicks on subtitle button, it should open their storage from which they could choose the file which should get loaded and play in ExoPlayer.
Any help is appreciated, thanks!