Authentication for Videoview in android

前端 未结 3 1674
说谎
说谎 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条回答
  •  猫巷女王i
    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 params = new HashMap(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.

提交回复
热议问题