How to stop youtube video playing in Android webview?

56,150

Solution 1

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:

@Override
public 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 2

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.

@Override
public void onPause() {
    super.onPause();
    mWebView.onPause();
}

@Override
public 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");

Solution 3

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

@Override
public void onResume()
{
    super.onResume();
    webView.onResume();
}

@Override
public void onPause()
{
    super.onPause();
    webView.onPause();
}

Solution 4

The above solution failed for me on Samsung Galaxy S with Android 2.3.3. But I made success trying the below workaround:

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

I am forcing the webview to load a non existent html file. This seems to be forcing the webview to clean things up.

Solution 5

Try this:

@Override
public void onPause() {
    super.onPause();
    myWebView.onPause();
    myWebView.pauseTimers();
}

@Override
public void onResume() {
    super.onResume();
    myWebView.resumeTimers();
    myWebView.onResume();
}


@Override
protected void onDestroy() {
    myWebView.destroy();
    myWebView = null;
    super.onDestroy();
}
Share:
56,150
Droid
Author by

Droid

Updated on July 05, 2022

Comments

  • Droid
    Droid almost 2 years

    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.

    Code:

    webView = (WebView) findViewById(R.id.webview); 
    webView.getSettings().setPluginsEnabled(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(false); 
    webView.getSettings().setSupportZoom(false);
    webView.loadData(myUrl,"text/html", "utf-8");
    
  • png
    png about 12 years
    i was trying to put logic in "back key press " . but it didnt work. this one works
  • dakshbhatt21
    dakshbhatt21 over 10 years
    thank you !!! I was trying for last 8 hours and nothing works. But this is working like a charm !!!
  • grandouassou
    grandouassou over 9 years
    It seems like the WebView.onResume() and WebView.onPause() are not hidden anymore since API 11. See Ryoghurt's answer. On top of that if your fragment is in a ViewPager, you should also call WebView.onPause from Fragment.setUserVisibleHint().
  • bk138
    bk138 about 9 years
    ...and works better than onPause() which on some devices seems to do nothing.
  • 0xPixelfrost
    0xPixelfrost about 9 years
    Answer is not up to date. API has changed and webview.onPause() is now available. The correct seems to be: stackoverflow.com/questions/5946698/…
  • Sean Beach
    Sean Beach almost 9 years
    WebView.onPause() was added in API 11, making this the more-correct answer.
  • SpaceMonkey
    SpaceMonkey almost 9 years
    Such a common problem has such a complication solution.. why is Android so bad, I don't get it
  • Jarrod Smith
    Jarrod Smith almost 9 years
    Didn't work for me on API 19, AudioBoom embedded player continues playing audio.
  • Nilzor
    Nilzor almost 9 years
    @jarrodSmith try pauseTimers();. Maybe it's more javascript based.
  • Nilzor
    Nilzor almost 9 years
    Warning: This methods behaves like a static method although it's a instance method. ALL WebView instances will pause when you call mWebView.onPause on either.
  • Nilzor
    Nilzor almost 9 years
    Correction: comment above only valid for pauseTimers, not onPause. Tested on HTC One and One M8 w/Android 5.0.1, Chrome v43
  • bashan
    bashan over 8 years
    This solution doesn't work well. When I minimize my app the video on youtube does stop properly, but I when I go back to the page and press play on the video again and then minimize the app the video continues to play.
  • bashan
    bashan over 8 years
    This is not a good solution, since you will probably want to go back to the page again after a while and you expect the webview to be on the same state when you left it.
  • Reham
    Reham over 8 years
    did you find a solution for this problem? because i'm still facing it
  • Krzysztof Dubrowski
    Krzysztof Dubrowski over 8 years
    This solution does not take into account the newer APIs at all
  • Rajesh
    Rajesh over 8 years
    doesn't work for me.is there any solution to stop the video?
  • Manuel Schmitzberger
    Manuel Schmitzberger over 8 years
    In my application this solution is working, because I haven't the case where I go back to the FragmentActivity.
  • Steve
    Steve about 8 years
    On what page in Cordova's /platforms/android folder do you put this code? Under what lines?
  • Steve
    Steve about 8 years
    What's with the "m" in "mWebView"?
  • Bryan
    Bryan almost 8 years
    @Steve It is a naming convention outlined in the Android Code Style for Contributors documentation, it stands for 'member'. Though this code style is meant for the Android source code (not necessarily application code), many Android application developers have adopted it.
  • DH28
    DH28 almost 8 years
    Works with youtube video in webview. Thanks!
  • John Pang
    John Pang almost 8 years
    Need to call both webView.onPause() and webView.onResume() in the activity's / fragment's onPause() and onResume(). Otherwise, video is pause on first time but not on afterward.
  • John Pang
    John Pang almost 8 years
    Need to call both webView.onPause() and webView.onResume() in the activity's / fragment's onPause() and onResume(). Otherwise, video is pause on first time but not on afterward.
  • John Pang
    John Pang almost 8 years
    This shall be the better answer than above.
  • Neon Warge
    Neon Warge over 7 years
    @Steve, its also a nice trick to leverage the intellisense if you are using Android Studio (just like any other IDE like Visual Studio). When you are in your class and typed m, a popup list menu will appear listing all the variables you have in the class that starts with 'm'. You can use other letter if you want to.
  • Neon Warge
    Neon Warge over 7 years
    Is it safe to combine dangels answer with this one to make sure I cover every cases?
  • Roxx
    Roxx over 6 years
    what is the file name to update. Also will it work on latest API 19-26
  • rogerdpack
    rogerdpack over 6 years
    The other answer was updated to "adopt" this, for followers :)
  • rogerdpack
    rogerdpack over 6 years
    This worked fine for me (at least with youtube embed) without the need for pauseTimers, FWIW. Also note that John Pang's comment has been implemented into it now.
  • Silas S. Brown
    Silas S. Brown about 6 years
    Although the developer documentation says onPause was added in API 11, it has actually been there since API 1 but undocumented. As evidence of this, the old Eclipse-based ADT, when set to an API target of 1, does not complain about it as it complains about other methods above the target API. Therefore, even if you target API 1, you can still use this answer by itself without needing any fallbacks.
  • Mackovich
    Mackovich almost 5 years
    Unfortunately that does not work for me (onPause / onResume).
  • hsu.tw
    hsu.tw almost 5 years
    the method is not hiden anymore.
  • rayworks
    rayworks over 4 years
    Works for me, but this method requestAudioFocus is deprecated since API level 26.
  • sommesh
    sommesh over 4 years
  • hsu.tw
    hsu.tw about 4 years
    Cooool. In 2011, you got a solution.
  • Asiel Diaz Benitez
    Asiel Diaz Benitez about 2 years
    this is the onPause() implementation but what about onResume() ?? how do you resume the music the webview might be playing etc? I tested this with a game that have background music and it pauses the music when minimized but then the game is silent when you resume the webview