Automatic ducking not working for text-to-speech (Android 8+)

600 views Asked by At

As described in documentation, the automatic ducking feature has been introduced in Android 8.0. Ducking means that if your music application has been interrupted by some short sound (like a notification, for ex.), your application will continue playing music, but music volume will be temporary lowered while short sound is playing.

I use a text-to-speech engine to read long articles, and want it to behave similar to music player, i.e. I want it to be automatically ducked by the system in android 8.0

I've got ducking to work fine without any additional code on Android 8.0 for music playing, but not for text-to-speech playing.

Here is a sample code

public class PlayerService extends Service {
    
    //private MediaPlayer mp;
    TextToSpeech tts;


    @Override
    public void onCreate() {
        super.onCreate();

        //...
        //create foreground notification stuff omitted...
        //...

        AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
        am.requestAudioFocus(listener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

/*
        mp = MediaPlayer.create(this, R.raw.music);
        mp.setLooping(true);
        mp.start();
*/

        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    tts.setAudioAttributes(
                            new AudioAttributes.Builder()
                                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                                    .build()
                    );
                }

                tts.speak("IF you happen to have read another book about Christopher Robin, you may remember that he once had a swan (or the swan had Christopher Robin, I don't know which) and that he used to call this swan Pooh. That was a long time ago, and when we said good-bye, we took the name with us, as we didn't think the swan would want it any more. Well, when Edward Bear said that he would like an exciting name all to himself, Christopher Robin said at once, without stopping to think, that he was Winnie-the-Pooh. And he was. So, as I have explained the Pooh part, I will now explain the rest of it. ",
                            TextToSpeech.QUEUE_ADD, null);                    
            }
        });

    }


    @Override
    public void onDestroy() {

        tts.stop();

/*
        mp.stop();
        mp.release();
*/

        AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
        am.abandonAudioFocus(listener);

        super.onDestroy();
    }


    private AudioManager.OnAudioFocusChangeListener listener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            //nothing to do here    
        }
    };

}

If I'd remove text-to-speech stuff and uncomment MediaPlayer lines, ducking will work fine for music playing. But if using text-to-speech as presented, no ducking is happening - during the notification sound text-to-speech continues playing as usually, and no volume change is performed.

I want ducking (volume change) to happen also if I use text-to-speech. What am I doing wrong?

0

There are 0 answers