是否可以更改Android的默认MediaPlayer User-Agent
字符串?如果是这样,怎么样?
答案 0 :(得分:1)
String referer="Referer: http://www.google.com/\r\n",
useragent="USER-AGENT: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36\r\n";
Map<String,String>mhead;
VideoView vp;
void prepareHeader(){
mhead=new HashMap<String,String>();
if(android.os.Build.VERSION.SDK_INT >20){
//if 5.X or higher, Fix User-Agent be force set by system
mhead.put(referer.split(": ")[0],referer.split(": ")[1]);
mhead.put(useragent.split(": ")[0],useragent.split(": ")[1]);//"User-Agent" => "USER-AGENT"
}else{
//if 4.X or lower, Fix Referer cannot be set
mhead.put(useragent.split(": ")[0],useragent.split(": ")[1]+referer);//"User-Agent: ... \r\nReferer: ..."
}
setVideoURI(Uri.parse(path),mhead);
}
void setVideoURI(Uri uri,Map<String,String>mHeaders){
try{
Class vpclass=Class.forName("android.widget.VideoView");
Method[]ms=vpclass.getMethods();
Method mTarget=null;
for(Method m:ms){
if(m.getName().equals("setVideoURI"))
if(m.getParameterTypes().length==2) mTarget=m;
}
if(mTarget==null){
//AlertDialog(this,"Set Headers Fail","Sorry... your device is not support this way.","ok");
}else{
mTarget.invoke(vp,new Object[]{uri,mHeaders});
}
}catch(Exception e){
//AlertDialog(this,"Set Headers Fail",Arrays.toString(e.getStackTrace()),"ok");
}
}
我已经过测试,它的工作地点是:
宇龙酷派8722V Android 5.1
Nox AVD Android 4.4
希望在你的设备上工作!
答案 1 :(得分:0)
您只需要覆盖HTTPDataSource.cpp(请参阅HTTPDataSource :: read_at)。 然后将它注入媒体播放器的核心=)(不是那么容易我需要说的) 首先看一下这项研究 - The flow of events during the Media Player Creation in Android Framework