Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto start a video using exoplayer?

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.

like image 451
tommy chheng Avatar asked Mar 17 '17 14:03

tommy chheng


People also ask

How do you play the next ExoPlayer video?

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.

Can I play YouTube video in ExoPlayer?

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.

What's the difference between exo player and VLC player?

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.


2 Answers

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);
}
like image 123
Charlie Avatar answered Oct 07 '22 20:10

Charlie


just use:

player.setRepeatMode(Player.REPEAT_MODE_ALL);
like image 2
Omar Othman Avatar answered Oct 07 '22 20:10

Omar Othman