Android Studio Text-to-Speech не работает - PullRequest
0 голосов
/ 21 марта 2019

Я смотрел уроки и копировал точно, но текст в речь выдает ошибку. Вот код:

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 голосов
/ 23 марта 2019

Вы можете решить непосредственную проблему, выполнив это:

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

}
...