使用带有基本认证url的VideoView



我想知道如何从经过身份验证的URL正确显示视频到VideoView。我的代码在棒棒糖(5.0)之前运行。现在,在编译到api 21和22之后,出现了错误。我已经尝试编译回API 19,但我使用的appcompat库打破了这么多代码。下面是我的示例代码:

                Uri videoUri = Uri.parse("https://user:12345@sample.com/dir1/dir2/dir3/myVideo.mp4");
                vidAtt.setVideoURI(videoUri);

I already tried

vidAtt.setVideoURI(videoUri, headers);

,但最小的API是21,而我的API是16。我尝试了生成的URI并将其粘贴到浏览器中,它可以工作。它只是在设备上不起作用。我还尝试将URI作为意图传递,以便它可以在外部打开视频链接,无论是使用库存视频播放器还是第三方视频播放器。

任何帮助都会很感激。提前感谢!

你必须使用setvideuri - method,这只适用于反射:

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

将basicAuthentication替换为Basic Authentication Header

你可以试试这个吗

try {
    VideoView.setVideoPath("https://user:12345@sample.com/dir1/dir2/dir3/myVideo.mp4"); 
    } catch (Exception e) {
    e.printStackTrace();
   }
   VideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    // Close the progress bar and play the video
    public void onPrepared(MediaPlayer mp) {
        //             
        VideoView.start();
    }
   });

最新更新