Android Studio文本到语音转换不起作用

时间:2019-03-21 19:57:19

标签: java android text-to-speech

我看过教程并完全复制了内容,但文字转语音会出错。这是代码:

public void speak(String text){
    TextToSpeech text_to_speech;
    text_to_speech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS){
                int result = text_to_speech.setLanguage(Locale.ENGLISH);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
                    Log.e("TTS", "Language not supported");
                } else {
                    text_to_speech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
                }
            } else {
                Log.e("TTS", "Failed");
            }
        }
    });
}

错误是“变量text_to_speech可能未初始化”。

更新:错误仅指向int result = text_to_speech.setLanguage(Locale.ENGLISH);

1 个答案:

答案 0 :(得分:0)

您可以通过以下方法解决眼前的问题:

public class MainActivity extends AppCompatActivity {

    TextToSpeech text_to_speech; // declare tts here

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        speak("hello");
    }


    public void speak(final String text){ // make text 'final'

        // ... do not declare tts here

        text_to_speech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS){
                    int result = text_to_speech.setLanguage(Locale.ENGLISH);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
                        Log.e("TTS", "Language not supported");
                    } else {
                        text_to_speech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
                    }
                } else {
                    Log.e("TTS", "Failed");
                }
            }
        });
    }
}

我建议以这种方式进行设置,

public class MainActivity extends AppCompatActivity {

    // declare the tts here so it can be accesed from all functions
    TextToSpeech text_to_speech;

    // track whether the tts is initialized
    boolean ttsIsInitialized = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initilize the tts here once, (not in the speak function)
        text_to_speech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS){
                    int result = text_to_speech.setLanguage(Locale.ENGLISH);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
                        Log.e("TTS", "Language not supported");
                    } else {
                        ttsIsInitialized = true; // flag tts as initialized
                    }
                } else {
                    Log.e("TTS", "Failed");
                }
            }
        });

    }

    public void speak(String text){

        if (!ttsIsInitialized) {return;}

        text_to_speech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

}