4

I am playing an audio using MediaPlayer class. I have a requirement where audio played by my app should automatically stop when some other songs started playing on the device by the user. How to get notified that some other audio has started playing on the device in my app? Please advise.

UPDATED:

I tried this code, it works for the first time. I.e, when I launch my app and play an audio in my app, my app is playing an audio. Then, When I open some music player and play some other audio there, My app detects notification in "AUDIOFOCUS_LOSS". I am stopping my audio here. Then when again play audio in my app, and then coming back to music player and play some other audio there, this time my app doesn't get detected for this notification, so I couldn't do stop again my audio. This is my issue now.

Could someone help me to solve this please?

    public class MyActivity extends ActionBarActivity implements OnCompletionListener, OnPreparedListener, OnErrorListener, OnBufferingUpdateListener, AudioManager.OnAudioFocusChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hclsquad_fm);

        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
  }
 @Override
    public void onAudioFocusChange(int focusChange) {

        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_GAIN:
                Toast.makeText(MyActivity.this, "Focus GAINED", Toast.LENGTH_LONG).show();
                break;
            case AudioManager.AUDIOFOCUS_LOSS:
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                Toast.makeText(MyActivity.this, "Focus LOSS", Toast.LENGTH_LONG).show();
                if(mediaPlayer != null)
                    killMediaPlayer();
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                Toast.makeText(MyActivity.this, "Focus LOSS TRANSIENT", Toast.LENGTH_LONG).show();
                break;
            default:
        }
    }
Stella
  • 1,728
  • 5
  • 41
  • 95

3 Answers3

2

Try this code below. I found this answer here

OnAudioFocusChangeListener listener = new OnAudioFocusChangeListener() {
  public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
      // Lower the volume
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            // Raise it back to normal
       }    
  }    
}; 

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int request = am.requestAudioFocus(listener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
Community
  • 1
  • 1
dumazy
  • 13,857
  • 12
  • 66
  • 113
1

Responsible apps will always get audio focus before playing audio. I don't think it is possible for you to detect when another app plays audio if it does not request audio focus.

Instead of requesting audio focus in your onCreate, do it before you start to play audio:

private void play(){
    requestAudioFocus();
}
tachyonflux
  • 20,103
  • 7
  • 48
  • 67
0

You are loosing audio focus. You should call

requestAudioFocus();

to get control back.