exoplayer播放rtmp流

exoplayer一直在更新,已經和以前的版本不太一樣了,而且我這邊需要播放rtmp流所以就又重新搞了下exoplayer

如果只是引用的話就直接

implementation 'com.google.android.exoplayer:exoplayer:2.X.X'

或者根據自己的需求引用需要的部分

implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'

2.X.X版本的話可以在RELEASENOTES.md查找

exoplayer本體是不支持rtmp流的,在首頁點開extensions可以看到exoplayer的擴展,可以發現exoplayer已經支持很多東西了,點開rtmp找到引入方法

implementation 'com.google.android.exoplayer:extension-rtmp:2.X.X'

版本同上,這樣就可以播放rtmp流了

也可以把源代碼下載下來拷貝到自己的項目,我就是下載下來的,因爲我的播放器有特殊需求要改代碼

我只拷貝了core,dash,hls和rtmp的代碼

拷貝到項目後發現exoplayer引入了很多的其他的項目,沒辦法,我也只能跟着引入

implementation 'org.checkerframework:checker:3.0.1'
implementation 'org.checkerframework:checker-compat-qual:2.5.5'
implementation 'org.jetbrains.kotlin:kotlin-annotations-jvm:1.3.60'
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'net.butterflytv.utils:rtmp-client:3.1.0'

如果不知道要引入什麼的話可以打開各文件夾下邊的build.gradle就可以看到了,比如rtmp文件夾下的build.gradle內找到implementation 'net.butterflytv.utils:rtmp-client:3.1.0'

還是直接引入方便,沒特別要求還是不要拷貝代碼了

由於exoplayer和我以前的版本差別比較大,初始化代碼都不一樣了,所以簡單貼一下,以播放hls和rtmp爲例,默認播放hls

private void initPlayer(){
        Uri url = Uri.parse(videoUrl);

        TrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory();
        DefaultTrackSelector trackSelector = new DefaultTrackSelector(/* context= */ this, trackSelectionFactory);

        player = new SimpleExoPlayer.Builder(/* context= */ this)
                .setTrackSelector(trackSelector)
                .build();
        MediaSource videoSource;
        if("rtmp".equals(videoUrl.substring(0,4))){
            RtmpDataSource.Factory dataSourceFactory = new RtmpDataSourceFactory();
            videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(url);
        }else{
            DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
                    Util.getUserAgent(this, "ExoPlayer"));
            videoSource = new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(url);
        }
        player.prepare(videoSource);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章