Search code examples
androidandroid-videoviewbasic-authentication

Using VideoView with basic authenticated URLs


I'd like to know how to properly display video from an authenticated URL to VideoView. My code is functioning before Lollipop (5.0). Now, after compiling to APIs 21 and 22, the error occurs. I have tried compiling back to API 19 but the appcompat library I'm using break so many codes. Here's my sample code:

                Uri videoUri = Uri.parse("https://user:[email protected]/dir1/dir2/dir3/myVideo.mp4");
                vidAtt.setVideoURI(videoUri);

I already tried

vidAtt.setVideoURI(videoUri, headers);

but the minimum API is 21 while mine is API 16. I tried the generated URI and paste into the browser and it works. It just doesn't work in the device. I also tried passing the URI as an intent so that it can open the video link externally, whether using the stock video player or a third part one. It didn't work too.

Any help would be appreciated. Thanks in advance!


Solution

  • You have to use the setVideoURI-Method which is only available with Reflection:

    Method setVideoURIMethod = videoview.getClass().getMethod("setVideoURI", Uri.class, Map.class);
    setVideoURIMethod.invoke(videoview, Uri.parse(url), /*HashMap<String, String>*/ basicAuthentication;
    

    Replace basicAuthentication with your Basic Authentication Header.