android exoplayer implementation for m3u8 live streming

时间:2018-02-01 18:25:22

标签: android android-layout exoplayer

I am new to android development in eclipse ( because my computer is low config PC ). I will try making tv application like stream .m3u8 files over network. how to archive this with exoplayer? It is very confusing to me.

1 个答案:

答案 0 :(得分:0)

m3um3u8是媒体播放列表的文件类型,就是这样。内容将采用HLS

格式

HLS是:

  

Apple Inc实施的基于HTTP的媒体流通信协议

(但非Apple产品可以流式传输)

这是一个教程,https://possiblemobile.com/2016/03/hls-exoplayer/是旧版本的exoplayer (因为exoplayer最近经历了很多变化)。

以下是我用来播放m3u8文件的工作版本(MyURL是指向m3u8(播放列表)文件的http链接:

首先启动玩家:

    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

    TrackSelection.Factory videoFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new DefaultTrackSelector(videoFactory);
    LoadControl loadControl = new DefaultLoadControl();

    // Create Player
    player = ExoPlayerFactory.newSimpleInstance(getApplicationContext(), trackSelector, loadControl);

然后添加HlsMediaSource

        Handler mHandler = new Handler();
        String userAgent = Util.getUserAgent(this, "User Agent");
        DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory(
                userAgent, null,
                DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
                1800000,
                true);
        HlsMediaSource mediaSource = new HlsMediaSource(MyURL, dataSourceFactory, 1800000,
                mHandler, null);
        player.setPlayWhenReady(true);
        player.prepare(mediaSource);

使用HLS,对于m3u8很像使用带有默认MediaSource的exoplayer,除非您应该使用HlsMediaSource而不是