ExoPlayer2 - 如何使HTTP 301重定向工作? [英] ExoPlayer2 - How can I make a HTTP 301 redirect work?

查看:1012
本文介绍了ExoPlayer2 - 如何使HTTP 301重定向工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用ExoPlayer来传输一些音频。一切顺利,直到我遇到一个301 Moved Permanently重定向的URL。 ExoPlayer2默认不处理。

I started using ExoPlayer to stream some audio. All was well until I came across an URL that has a "301 Moved Permanently" redirect. ExoPlayer2 does not handle that by default.

我已经看过这个帖子: https://github.com/google/ExoPlayer/issues/423

I've already seen this thread: https://github.com/google/ExoPlayer/issues/423

他们说要添加新的 allowCrossDomainRedirects标记为HttpDataSource或UriDataSource。问题是我不使用这些类中的任何一个:

There they say to add the new "allowCrossDomainRedirects" flag to either a HttpDataSource or UriDataSource. The problem is that I don't use either of those classes:

//I am NOT using SimpleExoPlayer because I need a different renderer.
exoPlayer = ExoPlayerFactory.newInstance(renderers, trackSelector, loadControl);

final DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(
            context,
            Util.getUserAgent(context, applicationInfo.getAppName())
);

// Produces Extractor instances for parsing the media data.
final ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

// This is the MediaSource representing the media to be played.
MediaSource mediaSource = new ExtractorMediaSource(
            Uri.parse(media.getUriString()) /* uri */,
            dataSourceFactory,
            extractorsFactory,
            10,
            null /* eventHandler */,
            null /* eventListener */);

exoPlayer.prepare(mediaSource);

了解ExtractorMediaSource如何需要dataSourceFactory而不是DataSource。实际上我甚至无法在ExoPlayer2上找到类HttpDataSource和UriDataSource。看起来他们已被删除。

See how the ExtractorMediaSource requires a dataSourceFactory instead of a DataSource. In fact I can't even find the classes HttpDataSource and UriDataSource on ExoPlayer2. Looks like they have been removed.

因此我找不到添加帖子中提到的标志的方法。有人可以帮帮我吗?

Therefore I can't find a way to add the flag mentioned on the post. Can somebody help me?

推荐答案

问题中描述的问题是关于跨协议重定向(从http到https,反之亦然) )。 Exoplayer支持此功能,但您必须将 allowCrossProtocolRedirects 设置为 true 。默认情况下支持常规重定向(包括301重定向)。您收到的重定向很可能是跨协议重定向。

The problem described in the issue is about cross-protocol redirects (from http to https or vice versa). Exoplayer supports this, but you have to set allowCrossProtocolRedirects to true. Regular redirects (including 301 redirects) are supported by default. The redirect you're receiving is most likely a cross-protocol redirect.

要创建您正在调用的数据源:

To create the data source you're calling:

DefaultDataSourceFactory(Context context, String userAgent)

此构造函数创建 DefaultHttpDataSourceFactory ,其中 allowCrossProtocolRedirects 设置为 false

This constructor creates a DefaultHttpDataSourceFactory which has allowCrossProtocolRedirects set to false.

要更改此项,您需要致电:

To change this, you need to call:

DefaultDataSourceFactory(Context context, TransferListener<? super DataSource> listener,
  DataSource.Factory baseDataSourceFactory)

并使用您自己的 DefaultHttpDataSourceFactory allowCrossProtocolRedirects 设置为 true 作为 baseDataSourceFactory

And use your own DefaultHttpDataSourceFactory with allowCrossProtocolRedirects set to true as the baseDataSourceFactory.

例如:

String userAgent = Util.getUserAgent(context, applicationInfo.getAppName());

// Default parameters, except allowCrossProtocolRedirects is true
DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(
    userAgent,
    null /* listener */,
    DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
    DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
    true /* allowCrossProtocolRedirects */
);

DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(
    context,
    null /* listener */,
    httpDataSourceFactory
);

如果您需要更频繁地执行此操作,您还可以创建辅助方法:

If you need to do this more often you can also create a helper method:

public static DefaultDataSourceFactory createDataSourceFactory(Context context,
        String userAgent, TransferListener<? super DataSource> listener) {
    // Default parameters, except allowCrossProtocolRedirects is true
    DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(
        userAgent,
        listener,
        DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
        DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
        true /* allowCrossProtocolRedirects */
    );

    DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(
        context,
        listener,
        httpDataSourceFactory
    );

    return dataSourceFactory;
}

这将允许跨协议重定向。

This will allow cross-protocol redirects.

旁注:301永久移动意味着客户需要将其URL更新为新URL。 302 Found用于常规重定向。如果可能,请更新返回301 Moved Permanently的网址。

Sidenote: "301 Moved Permanently" means clients need to update their URL to the new one. "302 Found" is used for regular redirects. If possible, update the URLs that return "301 Moved Permanently".

这篇关于ExoPlayer2 - 如何使HTTP 301重定向工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆