остановка звука при звонке

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

The special class PhoneStateListener

The solution is related to a class named PhoneStateListener. I put it in the main activity class. This is the class to handle the Phone-State change. If there’s incoming / outcoming call, this class will be invoked.


This is the code part of the PhoneStateListener, for the complete source follow the link at the bottom of article.



//this is ONLY part of the code, refer bpottom of article 
//to download complete code
...
...
...

public class BacaFatihahActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        private MediaPlayer mp;
        ...
        ...
        ...
        //this is a code segmentation
        //THis two lines of codes need to be inserted to the onCreate method
        //This following codes dealing with the phone-state
        //detect voice incoming call
        //onCallStateChanged method will be executed if there's incoming
        //or outcoming call
        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    //INCOMING call
                    //do all necessary action to pause the audio
                    if(mp!=null){//check mp
                        setPlayerButton(true, false, true);
                        
                        if(mp.isPlaying()){ 
                            
                            mp.pause();
                        }
                    }
                    
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    //Not IN CALL
                    //do anything if the phone-state is idle
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    //A call is dialing, active or on hold
                    //do all necessary action to pause the audio
                    //do something here
                    if(mp!=null){//check mp
                        setPlayerButton(true, false, true);
                        
                        if(mp.isPlaying()){ 
                            
                            mp.pause();
                        }
                    }
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };//end PhoneStateListener
        
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
        ...
        ...
        ...
        
    }//end onCreate

}//end class
- See more at: http://blog.kerul.net/2012/01/how-to-pause-audio-while-call-is.html#sthash.7wrwt6Qs.dpuf

There are a few things you can do:
First of all, you can listen for changes in the call state using a PhoneStateListener. You can register the listener in the TelephonyManager:
PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            //Incoming call: Pause music
        } else if(state == TelephonyManager.CALL_STATE_IDLE) {
            //Not in call: Play music
        } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
            //A call is dialing, active or on hold
        }
        super.onCallStateChanged(state, incomingNumber);
    }
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
    mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
Remember to unregister the listener when it's no longer needed using the PhoneStateListener.LISTEN_NONE:
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
    mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}
Another thing you can do is listening for the broadcast android.intent.action.PHONE_STATE. It will contain the extra TelephonyManager.EXTRA_STATE which will give you information about the call.Take a look at the documentation here.
Please note that you'll need the android.permission.READ_PHONE_STATE-permission in both cases.
shareimprove this answer
   
Do I need to register the PhoneStateListener in my Activity that plays music? What if that activity has lost focus? Will the listener still work? –  user669231 Apr 10 '11 at 12:20 
1 
Yes the listener needs to be registered in the Activity which plays music, and it will be called if the activity has lost focus (as long as it has not been killed). However, it may be more appropriate to use a Service to play the music instead of an Activity as the Activity may be killed. –  Kaloer Apr 11 '11 at 7:14 
   
This worked for me. I created a service & registered the phonestatelistener in it. Now even if my activity loses focus(like user goes to the home screen by pressing the home button), the incoming calls get detected & I am able to stop & start music –  user669231 Apr 12 '11 at 18:40
2 
You should also request AudioManager::requestAudioFocus(), so when the focus has been granted you can play the stream; AudioManager.OnAudioFocusChangeListener::onAudioFocusChange(int ) depending on the change application should pause/play, refer to AudioManager and OnAudioFocusChangeListener documentation. –  Vamsi Jan 9 '12 at 11:14
   
@kaloer we need the use the phonestatelistener where we are using audio player activity or it has to be in sepereate one –  Karthick M Sep 13 '13 at 13:05