自动闪避不适用于文本到语音转换Android 8+

问题描述

documentation中所述,自动回避功能已在Android 8.0中引入。闪避是指,如果您的音乐应用程序已被某些短音(例如通知)打断,则您的应用程序将继续播放音乐,但是在播放短音时,音乐音量会暂时降低。

我使用文本语音转换引擎阅读较长的文章,并希望它的行为类似于音乐播放器,即我希望它能够被Android 8.0中的系统自动回避

在没有用于音乐播放的Android 8.0上没有任何其他代码的情况下,我可以轻松地工作,但对于文本到语音的播放则无法。

这是示例代码

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    
        }
    };

}

如果我要删除文字转语音内容并取消注释MediaPlayer行,则闪避可以很好地播放音乐。但是,如果使用所示的文本到语音转换,则不会发生任何闪避-在通知声音中,文本到语音转换将像往常一样继续播放,并且不会进行音量更改。

如果我使用文字转语音,我也希望避开(音量变化)。我在做什么错了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...