jiajunhui / PlayerBase

The basic library of Android player will process complex business components. The access is simple。Android播放器基础库,专注于播放视图组件的高复用性和组件间的低耦合,轻松处理复杂业务。
Apache License 2.0
2.92k stars 438 forks source link

How to custom useragent with exoplayer ? #95

Closed mdtuyen closed 4 years ago

mdtuyen commented 5 years ago

In lib

  if (extra! = null && extra.size ()> 0 &&
                 ("http" .equalsIgnoreCase (scheme) || "https" .equalsIgnoreCase (scheme))) {
             dataSourceFactory = new DefaultHttpDataSourceFactory (
                     Util.getUserAgent (mAppContext, mAppContext.getPackageName ()));
             ((DefaultHttpDataSourceFactory) dataSourceFactory) .getDefaultRequestProperties (). Set (extra);
         }

So userAgent cannot be custom (by extra) So I think you should find UserAgent in extra before initializing DataSource

jiajunhui commented 5 years ago

Try this one.

HashMap<String, String> extra = new HashMap<>();
extra.put("User-Agent","xxxx");
DataSource data = new DataSource("http://xxxx.com");
data.setExtra(extra);
mVideoView.setDataSource(data);
mdtuyen commented 5 years ago

In function makeConnection of DefaultHttpDataSource:

private HttpURLConnection makeConnection(
      URL url,
      @HttpMethod int httpMethod,
      byte[] httpBody,
      long position,
      long length,
      boolean allowGzip,
      boolean followRedirects)
      throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(connectTimeoutMillis);
    connection.setReadTimeout(readTimeoutMillis);
    if (defaultRequestProperties != null) {
      for (Map.Entry<String, String> property : defaultRequestProperties.getSnapshot().entrySet()) {
        connection.setRequestProperty(property.getKey(), property.getValue());
      }
    }
    for (Map.Entry<String, String> property : requestProperties.getSnapshot().entrySet()) {
      connection.setRequestProperty(property.getKey(), property.getValue());
    }
    if (!(position == 0 && length == C.LENGTH_UNSET)) {
      String rangeRequest = "bytes=" + position + "-";
      if (length != C.LENGTH_UNSET) {
        rangeRequest += (position + length - 1);
      }
      connection.setRequestProperty("Range", rangeRequest);
    }
    connection.setRequestProperty("User-Agent", userAgent);
    if (!allowGzip) {
      connection.setRequestProperty("Accept-Encoding", "identity");
    }
    connection.setInstanceFollowRedirects(followRedirects);
    connection.setDoOutput(httpBody != null);
    connection.setRequestMethod(DataSpec.getStringForHttpMethod(httpMethod));
    if (httpBody != null) {
      connection.setFixedLengthStreamingMode(httpBody.length);
      connection.connect();
      OutputStream os = connection.getOutputStream();
      os.write(httpBody);
      os.close();
    } else {
      connection.connect();
    }
    return connection;
  }

That mean userAgent from extra set first, so that it overried by Util.getUserAgent (mAppContext, mAppContext.getPackageName ())

jiajunhui commented 4 years ago

已支持