如何使用 exoplayer 自动启动视频?
How to auto start a video using exoplayer?
我在 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);
我在 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);