I have a video loaded in a com.google.android.exoplayer2.ui.SimpleExoPlayerView
view but I want to make it automatically start when the view loads. Right now, the user has to click the play button.
Show activity on this post. 3] Just check by clicking next button from the media controller if that works then you are done, now the videos will be played automatically once finished the current one.
ExoPlayer is an app-level media player built on top of low-level media APIs in Android. It is an open source project used by Google apps, including YouTube and Google TV.
VLC Media Player for Android is one free and open source cross-platform multimedia player. It can play almost any media files, discs, and streaming media. So it is a good choice. ExoPlayer is one open source project that plays media with minimal code.
SimpleExoPlayer works well with a SurfaceView, there are methods to set the surface of the player.
This is how I create the SimpleExoPlayer:
/** Create a default TrackSelector **/
TrackSelector trackSelector = new DefaultTrackSelector(new Handler());
/** Create a default LoadControl **/
LoadControl loadControl = new DefaultLoadControl();
/** Create the player **/
mPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector, loadControl);
/** Make the ExoPlayer play when its data source is prepared **/
mPlayer.setPlayWhenReady(true);
I hold these factories so I don't have to create them each time I set a new data source.
/** Produces Extractor instances for parsing the media data **/
mExtractorsFactory = new DefaultExtractorsFactory();
/** Produces DataSource instances through which media data is loaded **/
mDataSourceFactory = new DefaultDataSourceFactory(
context, Util.getUserAgent(context, "AppName")
);
I use the following method to set a new data source on the player. This method uses the factories created earlier.
For me, the String source
is a URI to an MP4 file held on the device's SD card. Having setPlayWhenReady(true)
earlier, once this video is prepared & ready to play it will begin immediately.
public void setDataSource(SurfaceView view, String source) {
stopMedia();
mPlayer.setVideoSurfaceView(view);
view.requestFocus();
// Create the media source
mVideoSource = new ExtractorMediaSource(Uri.fromFile(
new File(source)),
mDataSourceFactory, mExtractorsFactory, null, null);
// Prepare the player with the source.
mPlayer.prepare(mVideoSource);
}
just use:
player.setRepeatMode(Player.REPEAT_MODE_ALL);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With