Android AudioManager abandonAudioFocus not helped other apps to playmusic

Viewed 706

Working on Voip call app, Issue is start native music player and play song, and opened my app made call after call music player doesn't resume playing by getting focus. What could be the issue, Thanks in advance.

Note : Native call app doing well in this case

AudioFocusChangeListener :

AudioManager.OnAudioFocusChangeListener mListener 
    = new AudioManager.OnAudioFocusChangeListener() {
   @Override
   public void onAudioFocusChange(int i) {
      MLog.e(TAG, "onAudioFocusChange(" + i + ")");
   }
};
 am.requestAudioFocus(mListener, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);

After Use trying to give focus for other apps to resume other app work:

AudioManager am = getAudioManager();
am.abandonAudioFocus(mListener);
2 Answers

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);
}

I had same kind of problem. I was playing music with VLC and VoIP call started. My app asked audio focus for it with call:

myAudioManager.requestAudioFocus(null, streamType,
    AudioManager.AUDIOFOCUS_GAIN)

(or corresponding Build.VERSION.SDK_INT >= 26 call), where streamType was AudioManager.STREAM_RING or AudioManager.STREAM_VOICE_CALL.)

After the call finished, I properly abandoned audio focus with call

myAudioManager.abandonAudioFocus(null)

(or corresponding Build.VERSION.SDK_INT >= 26 call). However, music didn't continue to play.

I fixed the issue by replacing the request AudioManager.AUDIOFOCUS_GAIN with AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE.

Related