0

I am developing the music application . My Problem is that when i play the music from my app other music player songs are pause but when I play other music player songs when my player songs are play that time both player songs are mixed . My music player songs does not pause (It should be pause). I used following code for it . Please help ....

public void playAudio() {
    OnAudioFocus onAudioFocus=new OnAudioFocus(this);

    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.requestAudioFocus(onAudioFocus, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    MusicPlayerActivity.mediaPlayer.start();
}

class OnAudioFocus implements AudioManager.OnAudioFocusChangeListener {

    HomeActivity onAudioFocus;

    public OnAudioFocus(HomeActivity onAudioFocus) {
        this.onAudioFocus = onAudioFocus;
    }

    public void onAudioFocusChange(int focusChange) {

        switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
            MusicPlayerActivity.mediaPlayer.start();


            break;

        case AudioManager.AUDIOFOCUS_LOSS:
            if (MusicPlayerActivity.mediaPlayer.isPlaying())
                 onAudioFocus.stopAudio();

                break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            if (MusicPlayerActivity.mediaPlayer.isPlaying())
                 onAudioFocus.pauseAudio();

                break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            if (MusicPlayerActivity.mediaPlayer.isPlaying())
                 MusicPlayerActivity.mediaPlayer.setVolume(0.1f, 0.1f);
                break;

        }
    }
}
Sachin
  • 528
  • 5
  • 17
  • You don't do anything when you lose audio focus, so I'm not sure why you're expecting the audio from your app to be paused(?) – Michael Mar 19 '15 at 09:15
  • @Michael - Actually I do the above operations, by mistakenly I addded the comments in front of them. .... I have edited my source code please see it again .. – Sachin Mar 19 '15 at 09:20

0 Answers0