3

When i play a video in android webview it play perfectly but when i press the back button or home key than it does not stop the sound. it play in background.

When i remove my application from recent apps than this sound is stop otherwise it play in background.

I try all the solution given in below link. but no one is working for me. http://devsolvd.com/questions/how-to-stop-youtube-video-playing-in-android-webview

This problem is not occur in Android 4.1 In android 4.4.2(Kitkat) webview plays sound in background.

My Code is

@Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();   

        if (onResumeCall)
        {
            webView.onResume();
            setContentsData();

            if (contentHolders.size() > 0)
                selectItem(currentIndex, 0, FIRST_PAGE);
        }



        onResumeCall = true;
        //webView.onResume();
        Log.i("OnREsume", "OnREsume");


    }

@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        }

@Override
    public void onBackPressed()    //when back pressed then
    {
        // TODO Auto-generated method stub


        super.onBackPressed();
    }

I try many things but it is not working. Plz Help Me.

5 Answers5

2
    @Override
     public void onPause() {
       super.onPause();
       mWebView.onPause();
     }
Jay Savsani
  • 299
  • 1
  • 3
  • 16
1

you can use this

@Override  
protected void onPause() {  
    m_WebView.reload();  

    super.onPause();  
}  
Ziem
  • 6,579
  • 8
  • 53
  • 86
wovski
  • 11
  • 2
0

I think you need to override onBackPressed buttton

public void onBackPressed() {
   mWebView.onPause();
   super.onPause();
}
baderkhane
  • 307
  • 2
  • 10
0

You can try this, it's a code I used for a similar problem on 4.4.2:

@Override
    public void onBackPressed() 
    {
        webView.clearView();
        webView.freeMemory();
        webView.removeAllViews();
        webView.destroy();

    try {
          Class.forName("android.webkit.WebView")
                        .getMethod("onPause", (Class[]) null)
                        .invoke(webView, (Object[]) null);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO: handle exception
        }
        super.onBackPressed();
    }
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
0

I had the same problem and I resolved by javascript, doing that:

@Override
public void onPause(){
    super.onPause();
    webView.loadUrl("javascript:pauseSound()");
}

@Override
public void onResume(){
   super.onResume();
   webView.loadUrl("javascript:playSound()");
   webView.onResume();
}
aru30stm
  • 36
  • 3