我在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!
更多推荐
发布评论