如何使用exoplayer自动启动视频?

tom*_*eng 2 android exoplayer

我在com.google.android.exoplayer2.ui.SimpleExoPlayerView视图中加载了一个视频,但我希望在视图加载时自动启动它.现在,用户必须单击播放按钮.

Cha*_*lie 6

SimpleExoPlayer适用于SurfaceView,有一些方法可以设置播放器的表面.

这就是我创建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);
Run Code Online (Sandbox Code Playgroud)

我拥有这些工厂,因此每次设置新数据源时都不必创建它们.

/** 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")
);
Run Code Online (Sandbox Code Playgroud)

我使用以下方法在播放器上设置新的数据源.此方法使用先前创建的工厂.

对我来说,它String source是设备SD卡上保存的mp4文件的URI.有了setPlayWhenReady(true)前面,一旦这部影片准备和准备玩就会立即开始.

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);
}
Run Code Online (Sandbox Code Playgroud)

祝好运!