How to stop youtube video playing in Android webview? How to stop youtube video playing in Android webview? android android

How to stop youtube video playing in Android webview?


See the following post about WebView threads never stopping

Essentially you'll need to call the WebView's onPause method from your own Activity's onPause method.

The only trick with this is that you cannot call the WebView's onPause method directly because it is hidden. Therefore you will need to call it indirectly via reflection. The following code should get you started on setting up your own Activity's onPause method:

@Overridepublic void onPause() {    super.onPause();    try {        Class.forName("android.webkit.WebView")                .getMethod("onPause", (Class[]) null)                            .invoke(webview, (Object[]) null);    } catch(ClassNotFoundException cnfe) {        ...    } catch(NoSuchMethodException nsme) {        ...    } catch(InvocationTargetException ite) {        ...    } catch (IllegalAccessException iae) {        ...    }}

Note that the variable 'webview' in the try block above is a private instance variable for the class and is assigned to in the Activity's onCreate method.


Solution: 1 -After spending lot of time, I got the conclusion to pause the video which is playing with WebView <iframe> concept of HTML.

Just override the onPause() method on Activity or Fragment whereever you used WebView, and call it. It works for me.

@Overridepublic void onPause() {    super.onPause();    mWebView.onPause();}@Overridepublic void onResume() {    super.onResume();    mWebView.onResume();}

Solution: 2 -If above solution doesn't work for you then you can force WebView to load a non existent html file.

mWebview.loadUrl("file:///android_asset/nonexistent.html");


you also have to implement the onResume() method or second time it won't work

@Overridepublic void onResume(){    super.onResume();    webView.onResume();}@Overridepublic void onPause(){    super.onPause();    webView.onPause();}