简单使用exoplayer播放HLS

使用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 来暂停和播放

你可能感兴趣的:(简单使用exoplayer播放HLS)