gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:59:17 26 4
gpt4 key购买 nike

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

最佳答案

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

关于android - 如何使用 exoplayer 自动启动视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42860593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com