Android TTS does not speak

I am trying to introduce text-to-speech to android technology in my work, but I encounter a strange error. I do not hear any sound from my code. The talk method only works if I put it in the onInit method, otherwise it does not say.

My code is as follows:

public class GameOverActivity extends Activity implements OnInitListener {
private TextToSpeech talker;
....
talker = new TextToSpeech(this, this);  
say("Something",false);
...
   public void onInit(int status) {  
        if (status == TextToSpeech.SUCCESS) {
          talker.setLanguage(Locale.US);
        }
        else if (status == TextToSpeech.ERROR) {
            Toast.makeText(this,"Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
        }

void say(String text, boolean flush) {
         if(flush == true)
         {
        talker.speak(text,TextToSpeech.QUEUE_FLUSH,null);
         }
         if(flush == false)
         {
        talker.speak(text,TextToSpeech.QUEUE_ADD,null);
         }         
    }

The strange thing is that if I put the say method in onInit, it works great!

I carefully monitored the logarithm and here are the results:

TtsService.OnCreate () TT loading Started AudioTrack TTSService.setLanguage loaded in the USA setting the speech speed to 100

and then nothing happens.

Any idea what is wrong with the above code?

Thanks in advance!

+5
source share
4 answers

, , TTS . , .

"" - , , , , , , .

"" -, , :

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

TextToSpeech.OnInitListener .

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

, , , ​​ SD-, , USB . , USB , .

, - , . .

0

Another cause of this problem may be your TTS engine. Sometimes the default TTS engine on SAMSUNG phones is SAMSUNG Engine, which doesn’t work in some languages ​​like Persian (I don’t mean for Persian text, I try to read English text, it still doesn’t work, it’s strange, but it happens). To solve all this, you need to install the TTS mechanism on your code (or choose Setting -> Language input -> Text to speech -> Google Text-to-speechmanually)

0
source

All Articles