Android TTS doesn't speak

后端 未结 4 622
闹比i
闹比i 2021-01-04 21:20

I am trying to implement text to speech technology of android in my Activity but I face a strange error. I can\'t hear any sound, from my code. The speak method works only i

相关标签:
4条回答
  • 2021-01-04 21:54

    It is recommended that you implement TextToSpeech.OnInitListener from your main activity. try this

    public class GameOverActivity extends Activity implements TextToSpeech.OnInitListener {
    
    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
    
            int result = mTts.setLanguage(Locale.US);
            // Try this someday for some interesting results.
            // int result mTts.setLanguage(Locale.FRANCE);
            if (result == TextToSpeech.LANG_MISSING_DATA ||
                    result == TextToSpeech.LANG_NOT_SUPPORTED) {
                // Lanuage data is missing or the language is not supported.
                //Log.e(TAG, "Language is not available.");
            } else {
                // Check the documentation for other possible result codes.
                // For example, the language may be available for the locale,
                // but not for the specified country and variant.
    
                // The TTS engine has been successfully initialized.
                // Allow the user to press the button for the app to speak again.
                // mAgainButton.setEnabled(true);
                // Greet the user.
                //sayHello();
            }
        } else {
            // Initialization failed.
    
        }
    
    }
    
    private TextToSpeech mTts;
    }
    
    0 讨论(0)
  • 2021-01-04 21:57

    One problem I've had with text-to-speech is that if it is installed to the SD card, then it won't work when the USB is plugged up. So you might try unplugging your test device from the USB and see if that solves the problem.

    Another thing you might try is stepping through the program and seeing if you are altering your text-to-speech object in any way on accident. Set a break point on the talk part of your code and take a look at all of the variables in your talker object.

    0 讨论(0)
  • 2021-01-04 22:10

    Well another cause of this problem could be your TTS engine, Sometimes in SAMSUNG phones the default TTS engine is SAMSUNG Engine which doesn't work on some languages like persian (I don't mean for persian text, Even if you're trying to read an english text, it still doesn't work, it's strange but It happens). In order to solve it all you have to do is to set the TTS engine on your code (or select Setting -> Language input -> Text to speech -> Google Text-to-speech manually)

    0 讨论(0)
  • 2021-01-04 22:11

    After some more hours looking the code, I noticed that the problem is that TTS engine initialization takes some time. If initialization is not over, the speak method call will fail.

    If you "say" something on button click, you will probably won't need this, because user will take some time to think before pressing the button, and the initialization will be over.

    If you want to "say" something as soon initialization finishes, use this code :

    talker = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
    
            @Override
            public void onInit(int arg0) {
           if(arg0 == TextToSpeech.SUCCESS) 
               {
            talker.setLanguage(Locale.US);
                say(gameover,true);
                say(line,false);
                say(definition_string,false);
                }
            }
        });
    
    0 讨论(0)
提交回复
热议问题