Reputation: 18629
I have this code which works and the only problem with it is that if I leave the WebView and even close the app, and even press power on the phone, the audio from the podcasts keeps playing.
Would anyone know how to stop that?
Here is the code:
public class PodcastsActivity extends BaseActivity //implements ActionBar.OnNavigationListener
{
WebView webview = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
//setTheme(R.style.Theme_Sherlock_Light);
super.onCreate(savedInstanceState);
//setContentView(R.layout.podcasts);
webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//webSettings.setBuiltInZoomControls(true);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
//webSettings.getMediaPlaybackRequiresUserGesture();
webSettings.setAllowContentAccess(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setUseWideViewPort(true);
setContentView(webview);
webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");
}
// public void onBackPressed ( )
// {
// Class.forName("com.***.HTML5WebView").getMethod("onPause", (Class[]) null).
// invoke(html5WebView, (Object[]) null);
// }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack())
{
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onStop()
{
super.onStop();
// your code
webview.clearView();
}
Upvotes: 24
Views: 13785
Reputation: 724
The accepted answer by @quietmint needs to call both WebView's onPause()
and Activity's onPause()
. However, I want to keep the Activity visible, so the Activity's onPause()
cannot be called.
Inspired by the answer of @Md Imran Choudhury, I try to load an empty url and it works for me.
webView.loadUrl("");
Upvotes: 0
Reputation: 151
Same issue for the Jetpack Compose can be solved by capturing the WebView (https://google.github.io/accompanist/webview/) in onCreated
and then use it to call destroy()
.
val webViewState = rememberWebViewState(webViewUrl.value)
val webView = remember {
mutableStateOf<android.webkit.WebView?>(null)
}
//..inside Composable..
WebView(
state = webViewState,
captureBackPresses = true,
onCreated = { wv ->
webView.value = wv
}
)
//..on Back Press/Close Button..
webView.value?.destroy()
Upvotes: 2
Reputation: 9997
In my case WebView onPause()
and onResume()
is not working for me.
Solution: onPause I load the below URL to stop the audio:
webView.loadUrl("javascript:document.location=document.location");
Upvotes: 1
Reputation: 15379
Accepted answer didnt work for me - just using onPause
in a fragment was not enough to stop music on Android 9. I had to also do:
override fun onDestroyView() {
webview?.destroy()
super.onDestroyView()
}
Upvotes: 13
Reputation: 14153
You should call through to the WebView's onPause()
and onResume()
from your Activity's onPause()
and onResume()
, respectively.
Pauses any extra processing associated with this WebView and its associated DOM, plugins, JavaScript etc. For example, if this WebView is taken offscreen, this could be called to reduce unnecessary CPU or network traffic. When this WebView is again "active", call onResume().
There's also pauseTimers()
, which affects all of the WebViews within your application:
Pauses all layout, parsing, and JavaScript timers for all WebViews. This is a global requests, not restricted to just this WebView. This can be useful if the application has been paused.
Upvotes: 33