ExoPlayer show subtitles - android

I already searched but I couldn't find any solution to my problem.
I play a video source like this:
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getActivity(), Util.getUserAgent(getActivity(), "UA")));
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(source));
simpleExoPlayer.setPlayWhenReady(true);
simpleExoPlayer.prepare(videoSource);
This "source" may have a subtitle track (embedded, if there's one it's already in the video and I don't want to import a srt file), how do I detect if there's a subtitle track, get a list of them and show the subtitles at the bottom of the video?
Also, how do I personalize them? I would like to have the white on a black background.

Related

Android Exo Player - Trouble playing Video on Demand from API

I'm working with the Pac-12 API (college sports data) and trying to make a streaming video player.
The Pac-12 API (http://api.pac-12.com/v3/vod?page=0") gives back a list of video objects with urls for their Video's on Demand (VODs).
I have exo player set up in Android and it's working for videos with urls that end in .mp4.
But when I try to plug in the VOD urls it's giving an error that it cannot extract the file type.
I'm using exo player 2.8.4 which may or may not be the problem. But the newer version of exo player ( 2.9.0 and above ) has min sdk 26 (which my testing phone doesn't run).
I'm working on checking if that's the problem, but in the mean time I wanted to post this question in case anyone can help.
Here is my exo player set up. mediaUrl is the varaible that works with .mp4 but not the VOD url.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(
context, Util.getUserAgent(context, "RecyclerView VideoPlayer"));
String mediaUrl = mediaObjects.get(targetPosition).getMedia_url();
if (mediaUrl != null) {
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(mediaUrl));
videoPlayer.prepare(videoSource);
videoPlayer.setPlayWhenReady(true);
}
The VOD urls give no file type extension at the end, so I tried concatenating '.vod' onto the mediaUrl varaible, but no luck with that.
From reading around online it seems like VODs are supported by exo player, but I can't find much about how the setup might be different.
Heres a direct link to one of the Pac-12 VODs. This is the same url as what the API returns.
https://pac-12.com/videos/oregons-dana-altman-talks-andy-katz-about-recruitment-getting-fans-back-and-more

Using CastPlayer with Subtitles on Android

I'm attempting to use the MediaItem class to load videos and show subtitles on the exoplayer2 CastPlayer. I'm able to see subtitles on the phone before casting and the video plays fine when cast to a TV using application/dash+xml format. However, I'm not able to figure out how to get subtitles to appear on the TV. I have added the MediaItem.Subtitle using MediaItem.Builder().setSubtitles(subtitleList). Previously I was able to do so with the deprecated MediaInfo class. I can't see any methods in the CastPlayer class to turn them on or how to customize the PlayerControlView to add a subtitle/caption button. Any ideas are greatly appreciated.
List<MediaItem.Subtitle> subtitleList = new ArrayList<>();
MediaItem.Subtitle externalSubtitle = new MediaItem.Subtitle(Uri.parse(url), MimeTypes.TEXT_VTT,"en", C.SELECTION_FLAG_DEFAULT, C.ROLE_FLAG_SUBTITLE, "ENGLISH-EXTERNAL");
subtitleList.add(externalSubtitle);
MediaItem mediaItem = new MediaItem.Builder()
.setUri(url)
.setMediaMetadata(mediaMetadata)
.setMimeType(mimeType)
.setSubtitles(subtitleList)
.build();
castPlayer.setMediaItem(mMediaItem);

Play multiple URL at once in Android

How to play multiple URLs sequentially in android? I have a small streaming url 10s/each of one complete song. I need to play it one after another. Is there any way doing it so the music don't get lagged?
https://exoplayer.dev/media-sources.html read Advanced composition
MediaSource firstSource =
new ProgressiveMediaSource.Factory(...).createMediaSource(firstVideoUri);
MediaSource secondSource =
new ProgressiveMediaSource.Factory(...).createMediaSource(secondVideoUri);
// Plays the first video twice, then the second video.
ConcatenatingMediaSource concatenatedSource =
new ConcatenatingMediaSource(firstSource, firstSource, secondSource);
It looks like you are using the m3u8 playlist to play with MediaPlayer. However, you wouldn't able to do it. You can use ExoPlayer to play m3u8 list. Check this link
https://github.com/google/ExoPlayer
simpleExoPlayerView.setShowMultiWindowTimeBar(true);

seekTo(mTimeMilis) won't work properly when using ConcatenatingMediaSource in ExoPlayer

I have more than one video to play one by one. After Creating MediaSource of each video, All these are going to ConcatenatingMediaSource(mediaSources[]). Normally it play one by one. But when video is fast forward using seekTo(), 1st video is ok but other videos do not follow seekTo().
Suppose 1st video is 10s, 2nd 12s, 3rd 10s.
If I call seekTo((long)12*1000) it should play 2nd video with 2s forward. But it plays from the beginning of 2nd video.
Setting VideoSources
DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(),Util.getUserAgent(getApplicationContext(), "ExoPlayer"));
MediaSource mediaSource = new ExtractorMediaSource.Factory(defaultDataSourceFactory).createMediaSource(videoItem.getVideoUri());
videoItemArrayList.get(k).setVideoSource(mediaSource);
Concatenate Sources
MediaSource[] mediaSources = new MediaSource[videoItemArrayList.size()];
int j=0;
for(VideoItem item : videoItemArrayList){
mediaSources[j] = item.getVideoSource();
++j;
}
concatenatedSource = new ConcatenatingMediaSource(mediaSources);
Setup exoplayer
exoPlayer.prepare(concatenatedSource);
exoPlayer.seekTo(0);
exoPlayer.setPlayWhenReady(true);
exoPlayer.getPlaybackState();
Using exoplayer.seekTo(period) internally calls currentWindowIndex() internally of the source. While you are playing the first video in the concatenated mediasource you end up receiving windowIndex as 0. Use seekTo(windowIndex, time) to solve the issue.

how to add multiple subtitles in Exo Player 2

I want to show subtitles with ExoPlayer 2 . users can choose between languages (English,German, or Arabic) . Video links are HLS (.m3u8) and subtitles are .str files .
I couldn't find any samples to do this.
is there any sample?
The link I added as a comment to your original post will be how you'll build the UI around text track selection. Then to actually get the tracks to be added to your mp4 file (or whatever the format is), you'll want to use a MergingMediaSource. The simple version looks like so:
MediaSource videoSource = new ExtractorMediaSource(videoUri, ...);
MediaSource subtitleSource = new SingleSampleMediaSource(subtitleUri, ...);
// Plays the video with the sideloaded subtitle.
MergingMediaSource mergedSource = new MergingMediaSource(videoSource, subtitleSource);
You can merge multiple subtitle tracks into the video source. Many different file formats are accepted.
I got that particular code sample from this blog post - but I believe that same code is also in the ExoPlayer documentation. That code block combined with the sample code that I link to in my other answer here should be enough to get you some subtitles.
Please let me know if that works for you.

Categories

Resources