4

我已经搜索了很多解决此问题的方法,但显然我找不到。好吧,顾名思义,我有一个简单的 Android 应用程序,它有一个 Webview。

public class MainActivity extends Activity {
   protected FrameLayout webViewPlaceholder;
   protected WebView webView;
   final Context myApp = this;
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      // Initialize the UI
      initUI();
   }

   protected void initUI()
   {
    // Retrieve UI elements
    webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));

    // Initialize the WebView if necessary
    if (webView == null)
    {
        // Create the webview
        webView = new WebView(this);
        webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(true);
        webView.getSettings().setLoadsImagesAutomatically(true);

        // Load the URLs inside the WebView, not in the external web browser
        webView.setWebViewClient(new WebViewClient());
        //webView.setWebChromeClient(new CustomChromeClient(this));
        webView.setWebChromeClient(new WebChromeClient() {  
            @Override  
            public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)   
            {  
                new AlertDialog.Builder(myApp)  
                    .setTitle("javaScript dialog")  
                    .setMessage(message)  
                    .setPositiveButton(android.R.string.ok,  
                            new AlertDialog.OnClickListener()   
                            {  
                                public void onClick(DialogInterface dialog, int which)   
                                {  
                                    result.confirm();  
                                }  
                            })  
                    .setCancelable(false)  
                    .create()  
                    .show();  

                return true;  
            };
            public void onShowCustomView(View view, CustomViewCallback callback){
                super.onShowCustomView(view, callback);
                Log.i("ChromeCLient", "onshowCustomView");
                new AlertDialog.Builder(myApp)  
                .setTitle("javaScript dialog")  
                .setMessage("ajbdlsakndsland")  
                .setCancelable(true)  
                .create()  
                .show();  
            }
            @Override
            public void onHideCustomView() {
                super.onHideCustomView();
                Log.i("ChromeCLient", "onhideCustomView");
            }
        });  

        webView.getSettings().setJavaScriptEnabled(true);
        if (Build.VERSION.SDK_INT < 8) {
          webView.getSettings().setPluginsEnabled(true);
        } else {
          webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        }
        // Load a page
        webView.loadUrl("http://jsbin.com/ijixe4/3");
    }

    // Attach the WebView to its placeholder
    webViewPlaceholder.addView(webView);
}

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    if (webView != null)
    {
        // Remove the WebView from the old placeholder
        webViewPlaceholder.removeView(webView);
    }

    super.onConfigurationChanged(newConfig);

    // Load the layout resource for the new configuration
    setContentView(R.layout.activity_main);

    // Reinitialize the UI
    initUI();
}

@Override
protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);

    // Save the state of the WebView
    webView.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);

    // Restore the state of the WebView
    webView.restoreState(savedInstanceState);
}
}

我的问题如下。在 webview 中加载的站点是一个包含简单 iframe 嵌入 youtube 视频的站点。当我点击 youtube 视频上的全屏按钮时,它会改变我的应用程序方向,即使我在清单中设置:screenOrientation =“portrait”。退出全屏时,在播放视频时(在后台)继续播放,即使由于方向更改而重新创建了 web 视图。当方向从横向变为纵向时,我尝试在活动的 onDestroy() 方法中销毁 webview。但这没有帮助。我尝试将 CustomWebChromeClient 设置为 webview 并实现 onShowCustomView() 方法,但除非我使用 HTML5 视频,否则显然不会调用 onShowCustomView()。我'

你们还有其他解决这个问题/错误的方法吗?我真的不想使用 HTML5 视频标签。提前致谢。

4

2 回答 2

5

只需重写 onPause 和 onResume 方法并在 webView 上调用相同的方法,如下所示。

@Override
protected void onResume() {
    super.onResume();
    this.webView.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    this.webView.onPause();
}
于 2014-05-25T04:37:36.983 回答
1

在面临同样的问题时发现了这一点:

webView.loadData("", "text/html", "utf-8"); // in onDestroy/onFinish

它停止视频播放。可能会有所帮助。我现在不能说它是否对您有帮助,但是您必须在方向更改或其他方面进行尝试。<> BTW Youtube 有自己的 API,因此使用它而不是 WebView 可能更好

于 2013-03-14T16:02:04.933 回答