2015-07-22 75 views
2

我想知道如何正確顯示視頻從經過身份驗證的URL到VideoView。我的代碼在Lollipop(5.0)之前運行。現在,在編譯到API 21和22之後,發生錯誤。我已經嘗試編譯回API 19,但我使用的appcompat庫打破了這麼多的代碼。這裏是我的示例代碼:使用帶有基本身份驗證URL的VideoView

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

我已經嘗試過

vidAtt.setVideoURI(videoUri, headers); 

但最小的API爲21,而我的是API 16我試圖生成的URI並粘貼到瀏覽器和它的作品。它在設備中不起作用。我也嘗試傳遞URI作爲意圖,以便它可以在外部打開視頻鏈接,無論是使用股票視頻播放器還是第三部分。它也沒有工作。

任何幫助,將不勝感激。提前致謝!

回答

1

您必須使用setVideoURI-方法,僅適用於反思:

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

與基本認證頭更換basicAuthentication。

+0

這隻適用於API 21及以上版本。它在API 21之下不起作用,如16(4.1.1)或17(4.2.2)。 –

+0

我已經在21下面的API上使用它。該方法在較低的API版本中不可見。但是使用Reflection可以完成這項工作。 –

+0

你說得對。我試過其他設備和模擬器,它的工作原理。 –

0

你可以試試這個

try { 
    VideoView.setVideoPath("https://user:[email protected]/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(); 

    } 
    }); 
+0

我試過了,沒有任何反應。發生同樣的錯誤。 –