ExoPlayer基本使用和關鍵類流程

許佳佳233發表於2018-01-02

官網地址:http://google.github.io/ExoPlayer/

#例子
極簡的邏輯,介面上僅僅顯示一個SimpleExoPlayerView。

這裡寫圖片描述
##新增依賴

repositories {
    jcenter()
    google()
}
compile 'com.google.android.exoplayer:exoplayer:2.6.0'

##程式碼

不要忘記在AndroidManifest中新增網路許可權。

MainActivity:

public class MainActivity extends AppCompatActivity {

    private final String testUrl="http://119.84.101.207/videos/v0/20180101/9c/20/33369eec370be393dd555a5a20234c02.mp4?key=0ebb94883d2df6eeb873b2dd48a35f687&dis_k=2d8cd483e5d3cf71159fcdfddad137350&dis_t=1514877572&dis_dz=CT-QIYI_SHMinRun&dis_st=44&src=iqiyi.com&uuid=a795aea-5a4b3284-bd&m=v&qd_ip=65e30cfd&qd_p=65e30cfd&qd_k=ab6b3e8679e84cccd49bfc91d5975606&qd_src=02028001010000000000&ssl=1&ip=101.227.12.253&qd_vip=0&dis_src=vrs&qd_uid=0&qdv=1&qd_tm=1514877572862";
    SimpleExoPlayer mPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initExoPlayer();
    }

    private void initExoPlayer() {
        RenderersFactory renderersFactory=new DefaultRenderersFactory(this);
        DefaultTrackSelector trackSelector=new DefaultTrackSelector();
        LoadControl loadControl=new DefaultLoadControl();
        mPlayer= ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector,loadControl);

        SimpleExoPlayerView playerView=new SimpleExoPlayerView(this);
        playerView.setPlayer(mPlayer);
        setContentView(playerView);

        Uri mp4Uri=Uri.parse(testUrl);
        DefaultDataSourceFactory dataSourceFactory=new DefaultDataSourceFactory(
                this, Util.getUserAgent(this,"exoPlayerTest"));
        ExtractorsFactory extractorsFactory=new DefaultExtractorsFactory();
        MediaSource mediaSource=new ExtractorMediaSource(
                mp4Uri,dataSourceFactory,extractorsFactory,null,null);
        mPlayer.prepare(mediaSource);
    }


    @Override
    protected void onDestroy() {
        mPlayer.release();
        super.onDestroy();
    }
}

#關鍵類流程圖

如果對播放器原理完全不理解的同學可以看下此文章:https://www.jianshu.com/p/82e778eb618b

這裡寫圖片描述

相關文章