WebView-YouTube视频在旋转时在后台播放并最小化 [英] WebView - Youtube videos playing in background on rotation and minimise

查看:79
本文介绍了WebView-YouTube视频在旋转时在后台播放并最小化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用WebView时遇到问题,基本上我是在一个将视频嵌入到某个地方的论坛中加载的,如果您播放视频,然后旋转设备,则视频会一直在后台播放,您可以停止该视频.当您最小化应用程序时,也会发生这种情况.有办法阻止这种情况吗?

I have an issue with WebView, basically I am loading in a forum that has embedded videos in places, if you play a video then rotate the device the video keeps playing in the background and you can get to it to stop it. This also occurs when you minimize the app. Is there a way to stop this?

这是我正在使用的WebView代码.

This is my WebView code I am using.

super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        this.view = (WebView) findViewById(R.id.webView);

        view.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != null && url.contains("grcade")) {
                    view.loadUrl(url);
                    view.getSettings().setJavaScriptEnabled(true);
                    return true;
                } else {
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                }
            }
        });

        view.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }


        });

        if(savedInstanceState == null){

            String url = "http://grcade.co.uk";
            view.getSettings().setJavaScriptEnabled(true);
            view.loadUrl(url);
            view.getSettings().setSupportZoom(true);
            view.getSettings().setBuiltInZoomControls(true);
            view.getSettings().setDisplayZoomControls(false);
            view.getSettings().setLoadWithOverviewMode(true);

            view.setBackgroundColor(Color.WHITE);

        }

        final SwipeRefreshLayout pulltoRefresh = (SwipeRefreshLayout) findViewById(R.id.pullToRefresh);

        pulltoRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                view.reload();
                pulltoRefresh.setRefreshing(false);

            }
        });
    }

    @Override
    public void onSaveInstanceState (Bundle outState)
    {
        super.onSaveInstanceState(outState);
        view.saveState(outState);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        view.restoreState(savedInstanceState);
        view.getSettings().setJavaScriptEnabled(true);
    }

    //Main WebView Panel End

任何帮助将不胜感激,因为我很困惑,我停止了它的返回过程,但是相同的方法无法正常工作,因为添加它会导致应用恢复为默认的Webview URL,而不是重新加载用户所在的页面.

Any help would be appreciated as I am very confused, I am stopped it occuring with going back but the same method wont work as adding it in causes the app revert to the default webview URL rather than reloading the page the user was on.

谢谢.

推荐答案

暂停/恢复视频

执行此操作的简单方法是,当您希望视频停止播放时(即当应用进入后台时)暂停WebView.只需通过以下代码即可完成:

Pausing/Resuming the Video

The simple way to do this is to pause the WebView when you wish the video to stop playing (i.e. when the app goes into the background). This is simply accomplished by the following code:

WebView view = (WebView) findViewById(R.id.webView);
view.onPause();    // This will pause videos and needs to be called for EVERY WebView you create
view.pauseTimers(); // This will pause JavaScript and layout for ALL WebViews and only needs to be called once to affect all WebViews

处理此逻辑的最佳方法是将暂停代码放入Activity的onPause()方法中,并在onResume中根据用户的观点将活动返回时恢复WebView:

The best place to handle this logic is to put your pausing code in the onPause() method of your Activity, and to resume the WebView when the activity comes back in view of the user in onResume:

WebView mWebView; // Initialize this somewhere

@Override
protected void onPause(){
    super.onPause();
    if(mWebView != null){
        mWebView.onPause();
        mWebView.pauseTimers();
    }
}

@Override
protected void onResume(){
    super.onResume();
    if(mWebView != null){
        mWebView.onResume();
        mWebView.resumeTimers();
    }
}

您可能对 onPause 感兴趣, onResume 以及

You may be interested in the onPause and onResume as well as the pauseTimers/resumeTimers methods in the WebView.

如果您不希望在旋转屏幕时重新启动活动"(这将阻止重新创建WebView),则只需放置

If you don't want your Activity to restart when you rotate the screen (which will stop the WebView from being recreated) then just put

android:configChanges="orientation|screenSize|keyboardHidden|keyboard"

AndroidManifest文件中活动"的< activity> 标记中的

.这不是通常遵循的好习惯,但是当您使用复杂的本机视图(例如WebView)时,可以这样做(Android Web浏览器使用它来避免在旋转时重新创建).使用此标签时,将发生的只是布局大小调整为适合横向的情况,您将必须手动处理使用 -port -在您的资源中放置文件夹.如果您仅使用一种布局,那么这不是问题.

in the <activity> tag of your Activity in your AndroidManifest file. This is not a good practice to follow normally, but when you are using a complex native view like the WebView, it is okay to do (Android web browsers use this to avoid being recreated on rotate). What will happen when using this tag is that the layout will just be resized to fit the landscape orientation and you will have to manually handle any layout specific changes that were being handled using -port or -land folders in your resources. If you are only using one layout though then it is not a problem.

请参阅此问题/答案,以获取有关此问题的更多信息,请注意,不鼓励这样做,但是它会起作用.

Please see this question/answer for more info on this, and note that it is not encouraged but it will work.

这篇关于WebView-YouTube视频在旋转时在后台播放并最小化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆