Droid Droid - 2 years ago 774
Android Question

How to stop youtube video playing in Android webview?

How can I stop a YouTube video which is played in my webview? When I click on the back button the video doesn't stop and instead continues in the background.


webView = (WebView) findViewById(R.id.webview);
webView.loadData(myUrl,"text/html", "utf-8");

Answer Source

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:

public void onPause() {

    try {
                .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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download