ExoPlayer2 - How can I make HTTP 301 redirect work?

I started using ExoPlayer to stream some sound. Everything was fine until I came across a URL with a "301 Moved Permanentently" redirect. ExoPlayer2 does not process by default.

I already saw this topic: https://github.com/google/ExoPlayer/issues/423

It says to add a new allowCrossDomainRedirects flag to an HttpDataSource or UriDataSource. The problem is that I am not using any of these 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); 

See how ExtractorMediaSource requires a dataSourceFactory instead of a DataSource. Actually I can't even find the HttpDataSource and UriDataSource classes on ExoPlayer2. It looks like they have been deleted.

Therefore, I cannot find a way to add the flag mentioned in the message. Can someone help me?

+6
source share
1 answer

The issue described in this question is about cross-forwarding (from http to https or vice versa). Exoplayer supports this, but you must set allowCrossProtocolRedirects to true . By default, regular redirects are supported (including 301 redirects). The redirect you receive is most likely a cross-redirect.

To create a data source that you call:

 DefaultDataSourceFactory(Context context, String userAgent) 

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

To change this, you need to call:

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

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

For instance:

 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-forwarding.

Sidenote: β€œ301 Moved Permently” means customers need to update their URL to a new one. "302 Found" is used for regular redirects. If possible, update the URLs that return "301 moved forever."

+16
source

Source: https://habr.com/ru/post/1013851/


All Articles