2017-07-25 104 views
3

我想在網頁視圖中顯示youtube網址。但它不會加載到Web視圖中。Youtube視頻未在android的webview中加載

這是我的代碼。

 WebView web_view = (WebView) findViewById(R.id.web_view); 

    web_view.setWebViewClient(new WebViewClient()); 
    web_view.getSettings().setJavaScriptEnabled(true); 
    web_view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
    web_view.getSettings().setPluginState(WebSettings.PluginState.ON); 
    web_view.getSettings().setMediaPlaybackRequiresUserGesture(false); 
    web_view.setWebChromeClient(new WebChromeClient()); 

    web_view.loadUrl("https://www.youtube.com/watch?v=s8n16rns-iM"); 

視頻沒有加載到web view.It是顯示錯誤: [INFO:控制檯(16)] 「關鍵的 」目標densitydpi「 不支持」,來源:https://m.youtube.com/watch?v=s8n16rns-iM

回答

0

試試這個

String frameVideo = "<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/watch?v=ue80QwXMRHg&app=desktop\" frameborder=\"0\" allowfullscreen></iframe></body></html>"; 

WebView displayYoutubeVideo = (WebView) findViewById(R.id.mWebView); 
displayYoutubeVideo.setWebViewClient(new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     return false; 
    } 
}); 
WebSettings webSettings = displayYoutubeVideo.getSettings(); 
webSettings.setJavaScriptEnabled(true); 
displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8"); 
+0

我怎樣才能得到frameVideo此視頻網址的https:/ /www.youtube.com/watch?v=ue80QwXMRHg&app=desktop – Lassie

0

要獲取特定影片的HTML框架,使用此功能與視頻ID,

public String getHtmlfromVideoId(String videoId) { 
     String html = "<iframe class=\"youtube-player\" " + "style=\"border: 0; width: 100%; height: 96%;" 
       + "padding:0px; margin:0px\" " + "id=\"ytplayer\" type=\"text/html\" " 
       + "src=\"http://www.youtube.com/embed/" + videoId 
       + "?&theme=dark&autohide=2&modestbranding=1&showinfo=0&autoplay=1\fs=0\" frameborder=\"0\" " 
       + "allowfullscreen autobuffer " + "controls onclick=\"this.play()\">\n" + "</iframe>\n"; 
     return html; 
    } 

視頻ID通常位於YouTube網址的v參數之後。例如,https://www.youtube.com/watch?v=s8n16rns-iM&app=desktop的視頻ID爲s8n16rns-IM

一旦你的HTML框架,在WebView中直接加載它,

webView.loadData(html, "text/html", "UTF-8"); 
+0

我想在webview中顯示與該Url的總YouTube – Lassie