0

我有这段代码可用于显示 WebView 并使用它:

WebView webview = null;

@Override
public void onCreate(Bundle savedInstanceState) 
 {
  //setTheme(R.style.Theme_Sherlock_Light);
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.podcasts);


  webview = new WebView(this);
  webview.getSettings().setAppCacheEnabled(false);
  webview.getSettings().setJavaScriptEnabled(true);
  webview.setInitialScale(1);
  webview.getSettings().setPluginState(PluginState.ON);

  webview.setWebViewClient(new WebViewClient() {
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
     }
});




  //webSettings.setBuiltInZoomControls(true);


  WebSettings webSettings = webview.getSettings();
  webSettings.setJavaScriptEnabled(true);
  webSettings.setBuiltInZoomControls(true);
  //webSettings.getMediaPlaybackRequiresUserGesture();
  webSettings.setAllowContentAccess(true);
  webSettings.setEnableSmoothTransition(true);
  webSettings.setLoadsImagesAutomatically(true);
  webSettings.setLoadWithOverviewMode(true);
  webSettings.setSupportZoom(true);
  webSettings.setUseWideViewPort(true);

  setContentView(webview);
  webview.loadUrl("url_to_go_to");         

但是,当我尝试使用此代码指向 YouTube 频道时,它只会显示一个空白屏幕。知道为什么会这样吗?

谢谢!

4

1 回答 1

4

为了显示 HTML5 视频,您需要在 WebView 中启用以下内容:

WebView view;
... //initialize WebView
WebSettings webViewSettings = view.getSettings();
view.setWebChromeClient(new WebChromeClient(){}); //just added this
webViewSettings.setDomStorageEnabled(true);
webViewSettings.setAppCacheEnabled(true);
webViewSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webViewSettings.setDatabaseEnabled(true);
webViewSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");

您还需要放入android:hardwareAccelerated="true"您的 AndroidManifest。否则,YouTube 视频之类的 HTML5 内容将无法正常工作。

于 2013-07-25T17:46:07.393 回答