I have a WebView that hosts embedded videos. when I degrade the Webview the sound of the video keeps on playing. How can I stop this? I did try webview.destroy();
but that force closes the application when I try to open the WebView again.
After searching a lot, I found this thread. I did something similar: Just extend WebView and override onWindowVisibilityChanged . This way, the audio continues to play if the screen is locked or another app is opened.
Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.
The Android system webview custom cache file has been deprecated and removed in Android 13. New apps and any app updates will now use the operating system default cache location.
You must call the WebView's onPause()
and onResume()
methods for that purpose. You typically do it on your Activity's onPause()
and onResume()
but you can also do it whenever you are hiding the WebView in some way and its contents shoud also stop doing whatever they are doing, such as running Javascript or playing an HTML5 video.
If you need these methods in API levels prior to 11, you can use reflection like this: WebView.class.getMethod("onPause").invoke(myWebView);
I assume that "when I degrade the Webview" to be when you close the view. Anyway, I also tried the same approach - calling webview.destroy(), and had the same crash as you.
The only approach that worked was to call _webView.loadData("", "text/html", "utf-8"); from my activity finish() method.
(based on this answer: How do I stop Flash after leaving a WebView? which did not really work as the onDestroy was not being called until much later).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With