如何使用外星播放器自动启动视频



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

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

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

/** 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")
);

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

对我来说,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);
}

只需使用:

player.setRepeatMode(Player.REPEAT_MODE_ALL);

最新更新