2017-03-17 54 views

回答

2

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

我認爲這些工廠,所以我不必創建它們每個I設置一個新的數據源的時間。

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

祝你好運!

相關問題