Reputation: 29
I am working with audio website for webview app,
problem is the app freezes when phone screen is lock is on due to the onPause
method i am using:
public void onPause() {
super.onPause();
mWeb.onPause();
mWeb.loadUrl("about:blank");
}
Now if I don't use mWeb.loadUrl("about:blank");
, then the music keeps running in background
If I use it and the phone goes in sleep mode, the app freezes
How can I solve this where app should not freeze when the lock screen shows and also so the music does not play when the app is closed?
Upvotes: 1
Views: 1705
Reputation: 33
I used pauseTimers()
method to stop video/audio from playing in the background in the onPause()
method of the activity.And then in onResume()
method I used resumeTimers()
and WebView.onResume()
method to webview freezing. Also, I'd suggest don't use "about:blank" in onPause()
instead in onDestroy()
method of the activity. Here's my complete implementation:
@Override
protected void onPause() {
super.onPause();
stopVideo();
webview.pauseTimers();
}
@Override
public void onResume() {
super.onResume();
webview.resumeTimers();
webview.onResume();
}
private void stopVideo() {
try {
Class.forName("android.webkit.WebView")
.getMethod("onPause", (Class[]) null)
.invoke(webview, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
webview.loadUrl("about:blank");
webview.destroy();
webview = null;
}
Upvotes: 1