I am using FragmentStatePagerAdapter with Exoplayer. My main goal is to load the next page, however don't play the video in that page. Right now in my VideoFragment for the viewpager, I am playing the video inside
onCreateView
player = new SimpleExoPlayer.Builder(mContext).build();
player.setPlayWhenReady(true);
player.setRepeatMode(Player.REPEAT_MODE_ONE);
playerView.setPlayer(player);
addListenerToPlayer();
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext,
Util.getUserAgent(mContext, "yourApplicationName"));
Uri uri = Uri.parse(mUrl);
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
player.prepare(videoSource);
How do I setup my exoplayer and adapter so that it only plays the current screen?
Replace player.setPlayWhenReady(true); with player.setPlayWhenReady(false); in your initial configuration as this instructs the player instance to play the media as soon as it's ready to be played.
According to the docs on Player.STATE_READY:
The player is able to immediately play from its current position. The
player will be playing if getPlayWhenReady() is true, and paused
otherwise.
Moreover, you should setup either PageChangeListener or PageChangeCallback (depending on which version of ViewPager you're using) to monitor which page in the ViewPager is currently selected and which are not. For the selected page, you can set its corresponding exoplayer instance to play the video with player.setPlayWhenReady(true). It is also a good idea to pause the player in each of your fragment's onPause(...) lifecycle method.
Related
I am creating MP3 player using exoplayer.
I have online tracks with DRM(Widevine) protection so exoplayer call HttpMediaDrmCallback for get license key to play DRM protected content.
After set track to the exoplayer it's takes 5 second to calls executeKeyRequest from HttpMediaDrmCallback class, how can I decreased the time
Set tracks to the exoplayer
DefaultTrackSelector trackSelector = new DefaultTrackSelector(this);
ExoPlayer exoplayer = new ExoPlayer.Builder(this)
.setTrackSelector(trackSelector)
.build();
exoplayer.setMediaSources(mediaSources); // I already created this object before
exoplayer.prepare();
exoplayer.seekTo(0, C.TIME_UNSET);
exoplayer.setPlayWhenReady(true);
I have implemented Exoplayer in recyclerview. Every 3rd item in the recyclerview is the video view while others are just images.
What i needed is to play the videos automatically as the videos are muted. So there are chances to show 2 players at the same time, so i want to play 2 videos simultaneously if there are 2 video items are even half visible.
But the first video starts playing automatically and when user scrolls the 2nd video player become visible and starts playing, so the first video player stops or pauses the video.
Is there a way to play 2 videos at same time in Exoplayer ?
Here is my code used for the onBindViewHolder method.
public void bindVideo(ExoplyerViewHolder holder, String url) {
Uri videoUrl = Uri.parse(url);
MediaSource mediaSource = new ProgressiveMediaSource.Factory( new CacheDataSourceFactory(context, 100 * 1024 * 1024, 15 * 1024 * 1024))
.createMediaSource(videoUrl);
SimpleExoPlayer simpleExoPlayer = new SimpleExoPlayer.Builder(context).build();
simpleExoPlayer.setAudioAttributes(AudioAttributes.DEFAULT, true);
simpleExoPlayer.setPlayWhenReady(true);
simpleExoPlayer.setRepeatMode(Player.REPEAT_MODE_OFF);
simpleExoPlayer.setVolume(0);
simpleExoPlayer.prepare(mediaSource, true, true);
holder.playerView.setPlayer(simpleExoPlayer);
holder.playerView.setPlaybackPreparer(this);
holder.playerView.setUseController(false);
}
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);
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.
I am using "android.widget.MediaController" for VideoView. Not sure when to use "android.media.session.MediaController" and what the difference is between them. Any idea?
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
videoView.start();
The documentation explains the difference very well. One is a view that offers controls for a media player; the other is an object fo interacting with an ongoing media session.
Description of android.widget.MediaController:
A view containing controls for a MediaPlayer. Typically contains the buttons like "Play/Pause", "Rewind", "Fast Forward" and a progress slider. It takes care of synchronizing the controls with the state of the MediaPlayer.
Description of android.media.session.MediaController:
Allows an app to interact with an ongoing media session. Media buttons and other commands can be sent to the session. A callback may be registered to receive updates from the session, such as metadata and play state changes.