WebView with embedded video in the background

I have a webview that hosts embedded videos. when I degrade webview, the sound of the video continues to play. How can i stop this? I tried webview.destroy(); but this force closes the application when I try again to open the WebView.

+6
source share
4 answers

You must call WebView onPause() and onResume() for this purpose. Usually you do this in your onPause() and onResume() activities, but you can also do this whenever you somehow hide the WebView, and its contents also stop doing what they do, for example, runs Javascript or Plays HTML5 video.

If you need these methods in API levels up to 11, you can use reflection as follows: WebView.class.getMethod("onPause").invoke(myWebView);

+5
source

I assume that "when I degrade Webview" 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 calling _webView.loadData ("," text / html "," utf-8 "); from my finish () method.

(based on this answer: How to stop Flash after exiting WebView? which really didn't work, since onDestroy was not called until much later).

+4
source

What do you mean by: "when I degrade Webview, the sound of the video continues to play."

Is this a regular application? If so, you need to create the application manifest file using android: hardwareAccelerated = true (if you are doing this on ICS or JB). Without hardware acceleration, you just hear the sound, but the video is not visible (this is what sounds the way you see). The following is information about modifying the manifest file: http://developer.android.com/guide/topics/graphics/hardware-accel.html

0
source

You can also execute a Java script to pause the video / audio on the onPause lifecycle callback, in the example below I did this for audio (it worked). Hopefully changing the β€œsound” to β€œvideo” helps 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); } } 
0
source

Source: https://habr.com/ru/post/924872/


All Articles