looking for some solutions? You are welcome.

SOLVED: Issue with OnAudioFocusChangeListener inside OnCreate()

nikhil:

Below code not working inside OnCreate() method but it is working fine when pasting the code outside the scope of OnCreate(). why?

AudioManager.OnAudioFocusChangeListener aflist = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            switch (focusChange) {

                case AudioManager.AUDIOFOCUS_LOSS:
                    md.stop();
                    releaseMediaPlayer();
                    Toast.makeText(getApplicationContext(), "PERM_LOSS", Toast.LENGTH_SHORT).show();
                    break;

                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    // Temporary loss of audio focus - expect to get it back - you can keep your resources around
                    Toast.makeText(getApplicationContext(), "TEMP_LOSS", Toast.LENGTH_SHORT).show();
                    if (md.isPlaying()) {
                        md.pause();
                    }
                    break;

                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    //Reduce the volume for few seconds
                    Toast.makeText(getApplicationContext(), "DUCK", Toast.LENGTH_SHORT).show();
                    setVolumeControlStream(AudioManager.ADJUST_LOWER);
                    break;
            }
        }
    };



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent