Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...