2017-01-07 15 views
6

Tôi bắt đầu sử dụng ExoPlayer để phát trực tuyến một số âm thanh. Tất cả là tốt cho đến khi tôi đi qua một URL có một chuyển hướng 301 chuyển vĩnh viễn. ExoPlayer2 không xử lý theo mặc định.ExoPlayer2 - Làm cách nào để chuyển hướng HTTP 301 hoạt động?

Tôi đã nhìn thấy chủ đề này: https://github.com/google/ExoPlayer/issues/423

Ở đó, họ nói để thêm mới "allowCrossDomainRedirects" cờ hoặc là một HttpDataSource hoặc UriDataSource. Vấn đề là tôi không sử dụng một trong các lớp đó:

//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); 

Xem cách ExtractorMediaSource yêu cầu dataSourceFactory thay vì nguồn dữ liệu. Trong thực tế, tôi thậm chí không thể tìm thấy các lớp HttpDataSource và UriDataSource trên ExoPlayer2. Có vẻ như chúng đã bị xóa.

Vì vậy, tôi không thể tìm thấy cách thêm cờ được đề cập trên bài đăng. Ai đó có thể giúp tôi được không?

Trả lời

15

Sự cố được mô tả trong vấn đề là về chuyển hướng giao thức chéo (từ http đến https hoặc ngược lại). Exoplayer hỗ trợ điều này, nhưng bạn phải đặt allowCrossProtocolRedirects thành true. Chuyển hướng thông thường (bao gồm chuyển hướng 301) được hỗ trợ theo mặc định. Chuyển hướng bạn nhận được rất có thể là chuyển hướng giao thức chéo.

Để tạo nguồn dữ liệu bạn đang gọi điện thoại:

DefaultDataSourceFactory(Context context, String userAgent) 

constructor này tạo ra một DefaultHttpDataSourceFactory trong đó có allowCrossProtocolRedirects thiết lập để false.

Để thay đổi điều này, bạn cần phải gọi:

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

Và sử dụng riêng DefaultHttpDataSourceFactory với allowCrossProtocolRedirects thiết lập của bạn để true như baseDataSourceFactory.

Ví dụ:

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 
); 

Nếu bạn cần phải làm điều này thường xuyên hơn bạn cũng có thể tạo ra một phương pháp helper:

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; 
} 

này sẽ cho phép chéo giao thức chuyển hướng.

Sidenote: "301 Đã chuyển vĩnh viễn" có nghĩa là khách hàng cần phải cập nhật URL của họ sang URL mới. "302 Found" được sử dụng để chuyển hướng thường xuyên. Nếu có thể, hãy cập nhật các URL trả về "301 Đã chuyển vĩnh viễn".

+0

Cảm ơn mã mẫu và giải thích - được đánh giá rất nhiều! – gmcnaughton

Các vấn đề liên quan