locked
How to stop a Webview RRS feed

  • Question

  • User200627 posted

    Hi, I'm using a web view to play videos, but when I navigate away from the page the audio won't stop. I have came across a few pages here, and many on StackOverflow but they weren't much help.

    People at SO suggests that I should be overriding the OnPause method from my mainactivity, Tried that and it doesn't get called. So I managed to catch when the user navigates away from the page. From then I'm calling a method to clear MyWebViews.source to null, calling GC. But the Audio won't stop.

    Can anyone suggest how I can destroy the Webview? Or explain how the Android lifecycle works in accordance to Webviews?

    Friday, June 17, 2016 11:46 AM

All replies

  • User232391 posted

    hi, the following solution is for Android: in your activity:
    Window.SetFlags(WindowManagerFlags.HardwareAccelerated, WindowManagerFlags.HardwareAccelerated); and your webview: if (_webView == null) { _webView = new WebView(Activity) {Background = new ColorDrawable(Color.Transparent)};

                var custWebViewClient = new WebViewClient();
                _webView.SetWebViewClient(custWebViewClient);
                var webChromeClient = new WebChromeClient();
                _webView.SetWebChromeClient(webChromeClient);
                //var hardwareAccelerated = _webView.IsHardwareAccelerated;
                _webView.Settings.JavaScriptEnabled = true;
                _webView.Settings.CacheMode = CacheModes.CacheElseNetwork;
                _webView.Settings.SetPluginState(WebSettings.PluginState.On);
                _webView.Settings.SetAppCacheEnabled(true);
                _webView.Settings.SetAppCacheMaxSize(2048000);
                _webView.Settings.AllowFileAccess = true;
                _webView.Settings.JavaScriptCanOpenWindowsAutomatically = true;
                _webView.Settings.SetRenderPriority(WebSettings.RenderPriority.High);
                _webView.Settings.SetLayoutAlgorithm(WebSettings.LayoutAlgorithm.SingleColumn);
                _webView.Settings.SetSupportMultipleWindows(true);
                _webView.Settings.DomStorageEnabled = true;
                _webView.Settings.DefaultTextEncodingName = "utf-8";
    
                _webView.SetLayerType(LayerType.Hardware, new Paint());
                _webView.LoadUrl(_url);
            }
    
            return _webView;
        }
    

    and, don't test on simulators but on real devices. GOOD LUCK!

    Wednesday, July 13, 2016 11:53 AM
  • User200627 posted

    Hi @AceLv Thank you for the reply, I was on holiday so I just saw this.

    web view doesn't have an extension of Settings on mine. Are you using some package for that? Also the same with WebViewClient

    Thanks

    Wednesday, July 27, 2016 12:57 PM
  • User232391 posted

    Xamarin Android(Native in PCL) Mono.Android.dll; C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v6.0\Mono.Android.dll using Android.Webkit; Android.Webkit.WebView

    Thursday, July 28, 2016 10:37 AM
  • User380769 posted

    Can WebView be stopped for UWP?

    Thursday, January 10, 2019 3:20 PM
  • User384995 posted

    Yes, into a ContentPage this works :

        protected override bool OnBackButtonPressed()
        {
            try
            {
                this.Player.Source = new UrlWebViewSource() { Url = "about:blank" };
            }
            catch (Exception)
            {
            }
            return base.OnBackButtonPressed();
        }
    
    Sunday, April 28, 2019 9:19 AM