Exoplayer 2 Android无法播放HLS

时间:2018-07-24 11:25:03

标签: android exoplayer

因此,我试图在ExoPlayer 2上播放一些HLS。它工作正常,但突然出现403错误。 当我在野生动物园上运行url(“ http://open.live.bbc.co.uk/mediaselector/5/redir/version/2.0/vpid/b0bbnbp9/mediaset/audio-syndication/proto/http”)时,它可以正常工作,但是当我在Android应用中尝试该网址时却无法正常工作。

val bandwidthMeter = DefaultBandwidthMeter()
    val extractorsFactory = DefaultExtractorsFactory()
    val trackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
    val trackSelector = DefaultTrackSelector(trackSelectionFactory)

    val userAgent: String = Util.getUserAgent(applicationContext, "mediaPlayerSample")
    val httpDataSourceFactory = DefaultHttpDataSourceFactory(
            userAgent,
            null,
            DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
            DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
            true
    )
val mediaSource = ExtractorMediaSource.Factory(httpDataSourceFactory).setExtractorsFactory(extractorsFactory).createMediaSource(Uri.parse(url))
        mPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector)
        mPlayer.seekTo(contentPosition)
        mPlayer.prepare(mediaSource)
mPlayer.playWhenReady = true

错误消息:

07-24 12:19:41.784 28569-29226/com.twoversion E/ExoPlayerImplInternal: Source error.
com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 403
    at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:211)
    at com.google.android.exoplayer2.upstream.DataSourceInputStream.checkOpened(DataSourceInputStream.java:102)
    at com.google.android.exoplayer2.upstream.DataSourceInputStream.open(DataSourceInputStream.java:65)
    at com.google.android.exoplayer2.upstream.ParsingLoadable.load(ParsingLoadable.java:129)
    at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:308)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)

2 个答案:

答案 0 :(得分:1)

您的代码似乎很完美,只是您需要更改代码:

val mediaSource = ExtractorMediaSource.Factory(httpDataSourceFactory).setExtractorsFactory(extractorsFactory).createMediaSource(Uri.parse(url))

收件人,更新以下代码:

HlsMediaSource hlsMediaSource = new HlsMediaSource(Uri.parse(url),httpDataSourceFactory, 1800000,new Handler(), null);

mPlayer.prepare(hlsMediaSource)

答案 1 :(得分:0)

播放器提供源错误,因为您提供给播放器的网址不是有效的hls网址。

Safari能够播放文件,因为它可以找出实际的重定向URL并播放。

您提供的网址实际上是重定向到:

http://cp401492-vh.akamaihd.net/i/prod_af_mp4_heaacv1_48/iplayerstream/l2o/b0bbnbp9_22abe4cc-c52b-4cf5-a785-e7803e1fdd9a.mp4/master.m3u8?hdnea=st=1532432045~exp=1532453645~acl=/b0bbnbp9_22abe4cc-c52b-4cf5-a785-e7803e1fdd9a.mp4~hmac=0fe4da8ddc4bc2dacd92865be29097d0a251cfa3489a7c56603563a268fe2c21

这是有效的hls网址(具有m3u8文件)。尝试在exoplayer上播放它,它应该可以工作

编辑:

您可以通过将allowCrossProtocolRedirects = true传递给DefaultUriDataSource构造函数来在ExoPlayer中启用跨协议重定向。