2014-12-03 96 views

回答

0

請試試這個代碼工作對我來說

String item = "http://www.youtube.com/embed/"; 

String ss = "your url"; 
ss = ss.substring(ss.indexOf("v=") + 2); 
item += ss; 
DisplayMetrics metrics = getResources().getDisplayMetrics(); 
int w1 = (int) (metrics.widthPixels/metrics.density), h1 = w1 * 3/5; 
wv.getSettings().setJavaScriptEnabled(true); 
wv.setWebChromeClient(chromeClient); 
wv.getSettings().setPluginsEnabled(true); 

try { 
    wv.loadData(
    "<html><body><iframe class=\"youtube-player\" type=\"text/html5\" width=\"" 
    + (w1 - 20) 
    + "\" height=\"" 
    + h1 
    + "\" src=\"" 
    + item 
    + "\" frameborder=\"0\"\"allowfullscreen\"></iframe></body></html>", 
          "text/html5", "utf-8"); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

private WebChromeClient chromeClient = new WebChromeClient() { 

    @Override 
    public void onShowCustomView(View view, CustomViewCallback callback) { 
     super.onShowCustomView(view, callback); 
     if (view instanceof FrameLayout) { 
      FrameLayout frame = (FrameLayout) view; 
      if (frame.getFocusedChild() instanceof VideoView) { 
       VideoView video = (VideoView) frame.getFocusedChild(); 
       frame.removeView(video); 
       video.start(); 
      } 
     } 

    } 
}; 
相關問題