tags:

views:

478

answers:

2

Hello,

I am using android.media.MediaPlayer object to play audio files in my app. Everything works fine but when a phone call comes while a song is playing the app does not pause the media player and you can hear the song during the phone call. Please tell me how to pause media player on a phone call and again resume it after call is ended.

A: 

When your Activity (or service) is interrupted by the phone call place MediaPlayer#pause() into Activity#onPause and code that resumes playing into Activity#onResume I think the problem may be that the MediaPlayer is executed in its own thread and when your Activity is paused it doesn't mean that MediaPlayer is automatically paused as well

DroidIn.net
The problem with this is that if the user changes activities within the application it will also pause the music.
FlySwat
I agree with Mark below - at that point you can use PhoneStateListener to detect if pause is due to phone usage
DroidIn.net
+2  A: 

Use a PhoneStateListener to find out when the phone is in use.

CommonsWare