Does the notification in android not use audio focus to play the notification sound?

Viewed 91

I use audio focus in my app to play an audio file, but as I am testing the app against notifications it turns out that the audio focus for the app is not lost whenever a new message or a battery low notification pops up and the audio still plays with a reduced volume along with the notification sound even though I have not coded this behavior in the audio focus change listener rather I want the volume to be reduced to 0 for the Transient_May_Duck focus change which I think should be used by the notification sounds but it turns out that this is not the case for the notification sounds. So is it so that the system itself reduces the volume while the notification sound starts (without requesting audio focus) even though this is not something coded by the developer for such audio focus change.

I am testing the app on a Virtual device running Android Oreo.

This is the code I wrote for the audio focus change,

AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int i) {
            switch (i) {
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    song.setVolume(0.0f, 0.0f);
                    Toast.makeText(MainActivity.this, "Can_Duck, reducing Vol to 0.0", Toast.LENGTH_SHORT).show();
                    break;

                case AudioManager.AUDIOFOCUS_GAIN:
                    song.setVolume(1.0f, 1.0f);
                    song.start();
                    Toast.makeText(MainActivity.this, "Focus Gained, Ready Again", Toast.LENGTH_SHORT).show();
                    break;

                case AudioManager.AUDIOFOCUS_LOSS:
                    releaseResources();
                    audioManager.abandonAudioFocus(audioFocusChangeListener);
                    Toast.makeText(MainActivity.this, "Loss, Releasing Res", Toast.LENGTH_SHORT).show();
                    break;

                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    Toast.makeText(MainActivity.this, "Loss_Transient", Toast.LENGTH_SHORT).show();
                    song.pause();
                    break;
            }
        }
    };
0 Answers
Related