Authentication for Videoview in android
I'm using a Videoview to play http video.That Http video url needs Authentication.
So please let me know how authentication can be set to开发者_StackOverflow社区 the VideoView?If not is there any other alternative for viewing authenticated video.?
Thanks & Regards, Sree Harsha .
There is a hidden method in VideoView that allows setting HTTP headers. You can use reflection to access it. But it will only help if the server supports basic authentication
Method setVideoURIMethod = videoView.getClass().getMethod("setVideoURI", Uri.class, Map.class);
Map<String, String> params = new HashMap<String, String>(1);
final String cred = login + ":" + pwd;
final String auth = "Basic " + Base64.encodeBytes(cred.getBytes("UTF-8"));
params.put("Authorization", auth);
setVideoURIMethod.invoke(videoView, uri, params);
Of course since this is undocumented API it is not guaranteed to work properly, you should handle exceptions and have a fallback plan.
First you should know what kind of authentication is required by this server: http://unixpapa.com/auth/index.html
Second, depending of auth type, you should provide auth parameters (username/password) inside the URL. Usually this will be accepted (but not necessatilly, you should test): http://username:password@www.yourhostname.com/whatever
This are basic, http-style authentications. Modern sites use other options such as OpenID and OAuth. This are a bit harder to implement.
Anyhow, you should know type type of authentication, before you start looking for solution.
The reflection API access to the setViewUri(Uri, Map) worked for me.
I guess a safer alternative is to use the MediaPlayer.setDataSource(Context, Uri, Map):
A risk of using the reflection API is that the API might disappear in a future release...
精彩评论