I am not sure whether i got your question correctly or not , but let me explain.
Your working on a Voip call app and in your case when ever user playing some music and try to make a call from app , the native player music has to be pause and again after call is completed it has to resume the playback.
Are you making calls with the use of TelephonyManager ? Then you can look at this solution where you can listen for changes in the call state using a PhoneStateListener. You can register the listener in the TelephonyManager:
If so, When ever your making any call from your Voip call app, simply check for the isMusicActive in the AudioManager. It will give the active music playing in the phone including custom apps.
isMusicActive() Checks whether any music is active. from docs
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME , CMDPAUSE );
sendBroadcast(i);
}
Here if MusicActive then simply pass it and do your calling. Once your done with your calling start / pause the Music player again, It will works for all the players not only for Default music player.
Control the default music player of android with these are the commands
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String CMDPLAY = "play";
public static final String CMDPAUSE = "pause";
public static final String CMDPREVIOUS = "previous";
public static final String CMDNEXT = "next";
public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
public static final String CMDSTOP = "stop";
In your case, before making the voip call from your app, check any active music is there or not like this ,
private boolean isSomethingPlaying;
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(mAudioManager.isMusicActive()) {
// some music is playing, so make it pause and save the flag for further usage.
isSomethingPlaying = true;
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME , CMDPAUSE );
sendBroadcast(i);
}
After your call is completed , then in that function check whether u made any music pause by using broadcast with the flag.
if(isSomethingPlaying)
{
// before our call some music is playing. so make it play again.
isSomethingPlaying = false;
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME , CMDPLAY);
sendBroadcast(i);
}