3

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);
Guruprasad
  • 931
  • 11
  • 16

2 Answers2

1

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);
}
King of Masses
  • 18,405
  • 4
  • 60
  • 77
  • 2
    It did trick for me . but if(mAudioManager.isMusicActive()) is not true even if player is alive. but i sent broadcast like this, then it working ..thank u so much Intent i = new Intent(SERVICECMD); i.putExtra(CMDNAME , CMDPLAY ); sendBroadcast(i); – Guruprasad Nov 16 '17 at 10:01
  • Can you please tell me in which case if(mAudioManager.isMusicActive()) is true – Guruprasad Nov 16 '17 at 10:11
  • usually it has to be true whenever there is a audioplayer instance which is in Active state i mean playing. – King of Masses Nov 16 '17 at 10:23
  • When i made call in my app, native player went pause state and player is active. but after my call isMusicActive() is false – Guruprasad Nov 16 '17 at 10:26
  • Yes , it will goes to false , because now player is inactive and not playing, for that you need to keep a flag before making player pause and after completing the call just check that flag and make the player active otherwise leave it as it is – King of Masses Nov 16 '17 at 10:28
  • 1
    yes.. used your updated ..this is what i want. thanku – Guruprasad Nov 17 '17 at 10:15
  • Before call i took state of player is true by isMusicActive..while call in progress user killed music app , in this case after call player should not start. bcz of our broadcast its playing again. any solution for this pls. – Guruprasad Nov 21 '17 at 05:19
  • Good Point. Need to check for that. Meantime you can look for a way to determine the music player state by using the AudioManager. Vll get back to you – King of Masses Nov 21 '17 at 06:37
1

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Juha
  • 349
  • 2
  • 11
  • A small clarification: `AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE` is used when no other apps (or system components) should play anything while audio focus is held. `AudioManager.AUDIOFOCUS_GAIN_TRANSIENT` is more forgiving towards others. I would use the latter for calls. – A.Vynohradov Sep 27 '22 at 14:22