如何使用exoplayer自动启动视频?(How to auto start a video using exoplayer?)

编程入门 行业动态 更新时间:2024-10-27 21:18:33
如何使用exoplayer自动启动视频?(How to auto start a video using exoplayer?)

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

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.

最满意答案

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

祝你好运!

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

Good luck!

更多推荐

本文发布于:2023-04-28 16:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1332443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   自动启动   视频   exoplayer   video

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!