Reputation: 942
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.
Upvotes: 8
Views: 5748
Reputation: 703
You can also execute Java script to pause the video/audio on onPause lifecycle call back of the activity, in below example I have done it for audio (it worked). Hopefully changing 'audio' to 'video' does the trick for you.
@Override
protected void onPause() {
executeJavascript("javascript:document.querySelector('audio').pause();", new ValueCallback() {
@Override
public void onReceiveValue(Object value) {
Trace.d(TAG, value.toString());
}
});
super.onPause();
}
and
private void executeJavascript(String javascript, ValueCallback callback){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
mWebView.evaluateJavascript(javascript, callback);
} else {
mWebView.loadUrl(javascript);
}
}
Upvotes: 0
Reputation: 19100
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);
Upvotes: 7
Reputation: 441
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).
Upvotes: 5
Reputation: 129
What do you mean by: " when I degrade the Webview the sound of the video keeps on playing".
Is this a custom app? If so, you need to build your app manifest file with android:hardwareAccelerated=true (assuming you are doing this on ICS or JB). Without hardware acceleration you will just hear the audio but the video cannot be seen (which is what it sound like you are seeing). Here is some information on changing your manifest file: http://developer.android.com/guide/topics/graphics/hardware-accel.html
Upvotes: 0