簡單使用exoplayer播放HLS

❀卜卜ღ?Bruce發表於2019-03-12

使用exoplayer 簡單的播放HLS協議 程式碼如下:

        DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
        String userAgent = Util.getUserAgent(this, "ExoPlayerDemo");
        DefaultDataSourceFactory mediaDataSourceFactory = new DefaultDataSourceFactory(this, BANDWIDTH_METER,
                new DefaultHttpDataSourceFactory(userAgent, BANDWIDTH_METER));
        String fileName = "http://live.xmcdn.com/live/59/64.m3u8";
        Uri uri = Uri.parse(fileName);
        if (fileName.endsWith(".m3u8")){
            mediaSource = new HlsMediaSource.Factory(mediaDataSourceFactory)
                    .createMediaSource(uri, null, null);
        }

        TrackSelection.Factory trackSelectionFactory  = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
        DefaultTrackSelector trackSelector = new DefaultTrackSelector(trackSelectionFactory);
        DefaultRenderersFactory renderersFactory =
                new DefaultRenderersFactory(this, DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER);
        player = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector);
        player.addListener(this);
        player.setPlayWhenReady(true);
        if(mediaSource!=null) {
            player.prepare(mediaSource);
        }
複製程式碼

注意 播放控制 就是使用player.setPlayWhenReady 來暫停和播放

相關文章