2

I have implemented a VoIP application and everything works fine. When a user is playing a music on his device using Android default music player or any other third party apps, and my app starts to ring or a message is received, my app's ringtone is not played and the music player is still playing and this situation keeps happening even in incall and call termination states.

How do I detect when a music player (default player for this stage) is playing a music and how to pause and resume it on my app's ringing and call termination states?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Mahdi Sadeghi
  • 299
  • 4
  • 12

1 Answers1

3

An application should request Audio Focus when it needs to utilize the audio output, either with music or notification sounds. Once it has the priority of Audio Focus, it can use the sound output freely, while listening for focus changes. If Audio Focus loses the focus, it should immediately either kill the audio or lower it to a quiet level and only resume loud playback when it receives focus again.

If an application needs to output music, Audio Focus should be requested. The method requestAudioFocus() should be called from the AudioManager.

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

int result = audioManager.requestAudioFocus( this, AudioManager.STREAM_MUSIC,
                                            AudioManager.AUDIOFOCUS_GAIN);

if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) 
{
       // could not get audio focus.
}

Take a look on Handling Audio Sound from Overlapping with VOIP call

Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • It seems like what I want. I will try it and accept your answer later. Thanks. – Mahdi Sadeghi Jun 28 '14 at 08:11
  • Thanks for your answer. It was exactly what I wanted. I also found this SO question with similar answer [link](http://stackoverflow.com/questions/15390126/how-to-stop-other-apps-playing-music-from-my-current-activity) which helped me too. – Mahdi Sadeghi Jun 28 '14 at 09:44