Gaurav Mandlik
Gaurav Mandlik

Reputation: 578

Audio Focus is not working in android 9 and android 10 android?

 int result = audioManager.requestAudioFocus(focusChangeListener,
            AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        startMediaPlayer(songPath);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        audioManager.registerAudioPlaybackCallback(new AudioManager.AudioPlaybackCallback() {
            @Override
            public void onPlaybackConfigChanged(List<AudioPlaybackConfiguration> configs) {
                super.onPlaybackConfigChanged(configs);

                boolean isFoundPlayOtherMusic = false;

                if (configs.size() > 1) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        if (configs != null) {
                            for (int i = 0; i < configs.size(); i++) {
                                if (configs.get(i).getAudioAttributes().getUsage() == USAGE_MEDIA && (configs.get(i).getAudioAttributes().getContentType() == CONTENT_TYPE_MUSIC || configs.get(i).getAudioAttributes().getContentType() == CONTENT_TYPE_UNKNOWN)) {
                                    isFoundPlayOtherMusic = true;
                                }
                            }

                            if (isFoundPlayOtherMusic) {
                                controls.pauseControl(getApplicationContext());
                            } else {
                                if (audioFocus == AudioManager.AUDIOFOCUS_LOSS ||
                                        audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
                                        audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                                    controls.playControl(getApplicationContext());
                                }
                            }
                        }
                    } else {
                        controls.pauseControl(getApplicationContext());
                    }

                } else if (configs.size() == 0) {
                    if (audioFocus == AudioManager.AUDIOFOCUS_LOSS ||
                            audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
                            audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
                        controls.playControl(getApplicationContext());
                }
            }
        }, handler);
    }

here is the code that check the audio focus of audio manager, is audio manager gain to success focus then play the Music Player, I also implement the audioChangeFocusListener as below code.

private AudioManager.OnAudioFocusChangeListener focusChangeListener =
        new AudioManager.OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int focusChange) {
                switch (focusChange) {
                    case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                        System.out.println("===== AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK : " + focusChange);
                        break;
                    case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                        System.out.println("===== AUDIOFOCUS_GAIN_TRANSIENT : " + focusChange);
                        break;
                    case AudioManager.AUDIOFOCUS_GAIN:
                        System.out.println("===== AUDIOFOCUS_GAIN : " + focusChange);
                        if (!mp.isPlaying())
                            controls.playControl(getApplicationContext());
                        break;
                    case AudioManager.AUDIOFOCUS_LOSS:
                        System.out.println("===== AUDIOFOCUS_LOSS : " + focusChange);
                        if (mp.isPlaying())
                            controls.pauseControl(getApplicationContext());
                        break;
                    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                        System.out.println("===== AUDIOFOCUS_LOSS_TRANSIENT : " + focusChange);
                        if (mp.isPlaying())
                            controls.pauseControl(getApplicationContext());
                        break;
                    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                        System.out.println("===== AUDIOFOCUS_LOSS : " + focusChange);

                }
            }
        };

Audio Focus is not working in android 9 and 10 both. I am using audio play with notification in my app.

Audio Focus not Gain and Loss in Our app, is it possible to stop our app music when audio play from other app, and when I play audio in my app then stop other music player from other app, is it possible then how?

Upvotes: 0

Views: 574

Answers (1)

GrAnd1
GrAnd1

Reputation: 26

This is also a sore subject for me. Try adding play dummy audio https://stackoverflow.com/a/50678833

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            AudioTrack audioTrack = new AudioTrack(
                    new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_MEDIA)
                            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                            .build(),
                    new AudioFormat.Builder()
                            .setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
                            .setSampleRate(48000)
                            .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                            .build(),
                    AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT),
                    AudioTrack.MODE_STREAM,
                    AudioManager.AUDIO_SESSION_ID_GENERATE
            );
            audioTrack.play();
            audioTrack.stop();
            audioTrack.release();
        }

Upvotes: 1

Related Questions