How to auto start a video using exoplayer?

一个人想着一个人 提交于 2020-03-20 03:55:10

问题


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.


回答1:


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);
}



回答2:


just use:

player.setRepeatMode(Player.REPEAT_MODE_ALL);


来源:https://stackoverflow.com/questions/42860593/how-to-auto-start-a-video-using-exoplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!