Closed mdtuyen closed 4 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);
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 ())
已支持
In lib
So userAgent cannot be custom (by extra) So I think you should find UserAgent in extra before initializing DataSource