Authentication for Videoview in android

前端 未结 3 1663
说谎
说谎 2021-01-06 11:37

I\'m using a Videoview to play http video.That Http video url needs Authentication.

So please let me know how authentication can be

相关标签:
3条回答
  • 2021-01-06 12:19

    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.

    0 讨论(0)
  • 2021-01-06 12:29

    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.

    0 讨论(0)
  • 2021-01-06 12:38

    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...

    0 讨论(0)
提交回复
热议问题