I think this is possible in one case.
1. Some of the built-in music players in the Android device where they do this restrict music when the call is in TelephonyManager.EXTRA_STATE_OFFHOOK (OFFHOOK STATE), so there is no way to play background music using your own players and some other players like "poweramp music palyer "
2. Using the MediaPlayer class is also not possible (clearly indicated in the documentation)
3. This is only possible in one case, if your developing custom music player (without using the MediaPlayer class) that implements
AudioManager.OnAudioFocusChangeListener, using this, you can get the status of the audio master in the code "focusChange = AUDIOFOCUS_LOSS_TRANSIENT" below (this state causes when music is playing in the background when an incoming call arrives) this state is completely in the hands of developers to play or pause music. As in accordance with your requirements, since for the question you asked if you want to play music when the call is in the OFFHOOK STATE state, do not stop the music playing in the OFFHOOK state. And this is only possible when the headset is turned off.
AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT // Pause playback (during incoming call) } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { // Resume playback (incoming call ends) } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { am.unregisterMediaButtonEventReceiver(RemoteControlReceiver); am.abandonAudioFocus(afChangeListener); // Stop playback (when any other app playing music in that situation current app stop the audio) } } };
KomalG
source share