android tts TextToSpeech 没有声音

android tts TextToSpeech 没有声音

IT小君   2021-02-27T16:58:17

android 集成科大讯飞3.0 本地tts ,之前好好的 ,后来突然没声音了 ,症状 

 new TextToSpeech 不调用 OnInitListener 的onInit 方法

控制台提示:

I/TextToSpeech: Sucessfully bound to com.xiaomi.mibrain.speech 

W/TextToSpeech: speak failed: not bound to TTS engine

通过逐步注释还原发现是由于如下代码导致的

final UDPTask udp = new UDPTask(
                this);
        udp.execute("");
        udpTask = udp;

其中 public class UDPTask extends AsyncTask<String, String, String>

所以 新建AsyncTask会导致TextToSpeech 无法初始化成功

于是改为

//实例并初始化TTS对象
        mTextToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    //设置朗读语言
                    int supported = mTextToSpeech.setLanguage(Locale.CHINA);
                    if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
                        Toast.makeText(MainActivity.this, "不支持当前语言!", Toast.LENGTH_LONG).show();
                    }
                }
                MainActivity.this.initUDPTask();
            }
        });


即 TextToSpeech 创建成功后再 新建AsyncTask

点击广告?(赏赐笔者)
评论(0)