Genadinik
Genadinik

Reputation: 18629

Android WebView - when playing a podcast in the webview, after leaving the screen the sound does not stop

I have the weirdest bug. I am running this exact code to play a popular business podcast:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    WebView webview = new WebView(this);
    webview.getSettings().setAppCacheEnabled(false);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setInitialScale(1);
    webview.getSettings().setPluginState(PluginState.ON);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setAllowContentAccess(true);
    webSettings.setEnableSmoothTransition(true);
    webSettings.setLoadsImagesAutomatically(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setSupportZoom(true);
    webSettings.setUseWideViewPort(true);

    setContentView(webview);
    webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");           

And it works just fine, and I am able to play the podcasts, but the problem is that when I leave the screen, the podcast keeps on playing.

Would anyone know what needs to be done to prevent that behavior?

Thanks, Alex

Upvotes: 0

Views: 404

Answers (2)

Stewart
Stewart

Reputation: 496

Call myWebView.clearView() in the onStop() event of the activity.

The activity and therefore the webview is not necessarily destroyed when it is no longer visible. It is similar to just minimising a browser on the desktop.

Have a look at the activity lifecycle for more information.

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006539

Do something in onStop(), such as load a blank page into the WebView, or perhaps call clearView() on it.

The "bug" that you are seeing is no different than how desktop browsers behave. If you load that URL in a desktop browser, and you switch to a different tab in the browser, or minimize the browser window, the podcast keeps playing.

Upvotes: 2

Related Questions