AudioManager is what you are looking for. You can check this response: https://stackoverflow.com/a/16252044/3743245 Also the official documentation: AudioManager
And a small example of how to use it:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(focusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// other app had stopped playing song now , so you can do your stuffs now .
}
Them you add the focusChangeListener
listed in the link I've left you.