Pause the music player on the phone and resume it after a phone call in android

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

+4
source share
5 answers

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

+5
source

When your activity (or service) is interrupted by the MediaPlayer#pause() phone number in Activity#onPause and the code that resumes playing in Activity#onResume , I think the problem may be that MediaPlayer is running in its own thread and when your activity paused, this does not mean that MediaPlayer is automatically paused, but

+2
source

I had a similar problem. My HTC Desire HD will stop the music when I receive a phone call, but will not resume after a call. I found that this was caused by the Viber application, and when I uninstalled "Viber" everything became normal. Now that I get a call, the music stops and resumes after I end the conversation.

0
source

Introduce PhoneStateListener in your application like this:

 private PhoneStateListener mPhoneListener = new PhoneStateListener() { public void onCallStateChanged(int state, String incomingNumber) { try { switch (state) { case TelephonyManager.CALL_STATE_RINGING: pauseStream(); break; case TelephonyManager.CALL_STATE_OFFHOOK: break; case TelephonyManager.CALL_STATE_IDLE: resumeStream(); break; default: } } catch (Exception e) { Log.e(TAG, e.getMessage()); } } }; 

Then you need to install it in your onCreate () or in your onStartCommand () method in case of service:

  radioStream.setAudioStreamType(AudioManager.STREAM_MUSIC); radioStream.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer stream) { telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); telephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); isMusicPlaying = true; isServiceRunning = true; stream.start(); } }); try { radioStream.prepare(); } catch (IllegalStateException e1) { e1.printStackTrace(); streamErrorHandler(); } catch (IOException e1) { e1.printStackTrace(); streamErrorHandler(); } 

and you can implement your version of the following methods:

 public static void pauseStream() { if (isServiceRunning) { if (radioStream.isPlaying()) { isMusicPlaying = false; radioStream.pause(); } } else { Log.i(TAG, "service isn't running"); } } public static void resumeStream() { if (isServiceRunning) { if (!radioStream.isPlaying()) { isMusicPlaying = true; radioStream.start(); } } else { Log.i(TAG, "service isn't running"); } } 

radioStream is your static MediaPlayer object. I use this in a service, so you can leave isServiceRunning , but this should work for any case.

0
source

here is a permanent solution. Use an audio recorder to get focus sound. When a call arrives, it calls AUDIOFOCUS_LOSS_TRANSIENT, and you can pause the media player in this method. After the call, it calls the AUDIOFOCUS_GAIN method, so just call the mediaplayer.start method.

 private AudioManager.OnAudioFocusChangeListener afChangeListener = new AudioManager.OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { mMediaPlayer.start();} else if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){ mMediaPlayer.pause(); } 
0
source

All Articles