How to stop watching web videos in the background?

HTML5 video opened in the background does not stop. I open webview in fragment. I tried the following code:

public void onPause(){ mWebView.onPause(); try { Class.forName("android.webkit.WebView") .getMethod("onPause", (Class[]) null) .invoke(mWebView, (Object[]) null); } catch(ClassNotFoundException e) { e.printStackTrace(); } catch(NoSuchMethodException e) { e.printStackTrace(); } catch(InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } super.onPause(); } 

this code stops the sound, but the media player is still running in the background. When I open the same fragment again, the application will crash:

 V/MediaPlayer(1173): notify(100, -38, 0) callback on disconnected mediaplayer W/dalvikvm(1173): threadid=1: thread exiting with uncaught exception (group=0x41212438) E/AndroidRuntime(1173): FATAL EXCEPTION: main E/AndroidRuntime(1173): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? E/AndroidRuntime(1173): at android.view.ViewRootImpl.setView(ViewRootImpl.java:704) E/AndroidRuntime(1173): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345) E/AndroidRuntime(1173): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) E/AndroidRuntime(1173): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149) E/AndroidRuntime(1173): at android.view.Window$LocalWindowManager.addView(Window.java:552) E/AndroidRuntime(1173): at android.widget.MediaController.show(MediaController.java:338) E/AndroidRuntime(1173): at android.webkit.HTML5VideoFullScreen.showControllerInFullScreen(HTML5VideoFullScreen.java:387) E/AndroidRuntime(1173): at android.webkit.HTML5VideoViewProxy$VideoPlayer.end(HTML5VideoViewProxy.java:310) E/AndroidRuntime(1173): at android.webkit.HTML5VideoViewProxy.handleMessage(HTML5VideoViewProxy.java:450) E/AndroidRuntime(1173): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(1173): at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime(1173): at android.app.ActivityThread.main(ActivityThread.java:4961) E/AndroidRuntime(1173): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(1173): at java.lang.reflect.Method.invoke(Method.java:511) E/AndroidRuntime(1173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) E/AndroidRuntime(1173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) E/AndroidRuntime(1173): at dalvik.system.NativeStart.main(Native Method) D/skia(1173): --------- SkTypeface::CreateFromBuffer failed 19160 
+3
source share

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


All Articles