Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView with embedded video plays in background

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.

like image 801
Christian Avatar asked Sep 07 '12 17:09

Christian


People also ask

How can I make WebView keep a video or audio playing in the background?

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.

What can I use instead of WebView?

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.

Is Android WebView deprecated?

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.


2 Answers

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);

like image 175
cprcrack Avatar answered Sep 22 '22 13:09

cprcrack


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).

like image 31
Israel Roth Avatar answered Sep 23 '22 13:09

Israel Roth