2017-05-08 69 views
0

我在android studio中将文本转换为语音时出错。我已经初始化了代码,但它仍然没有返回语音输出。代码如下。android text to speech toast message

else if((match.contains("yes") || match.contains("yeah")) && defsele) { 
      //Toast toast = Toast.makeText(getApplicationContext(), "Default selection is done and program is starting", Toast.LENGTH_SHORT); 
      //toast.show(); 
      defsele=false; 
      switch (progno) { 
       case 1: 
        //Toast toast1 = Toast.makeText(getApplicationContext(),"The default settings for cotton cycle is done",Toast.LENGTH_SHORT); 
        //toast1.show(); 
        String cotton = "The cotton program is starting with the default values"; 
        tts.speak(cotton, TextToSpeech.QUEUE_FLUSH, null); 
        soak=true; 
        soakdef(); 
        break; 

tts.speak被取消并且不起作用。我如何使这项工作。初始化代码如下

tts = new TextToSpeech(this,this); 
    @Override 
public void onInit(int status) { 
    Log.d("Speech", "OnInit - Status ["+status+"]"); 
    if(status == TextToSpeech.SUCCESS){ 
     Log.d("Speech","Success"); 
     tts.setLanguage(Locale.ENGLISH); 

我是非常新的android编程,并将不胜感激任何帮助。

感谢高级!!!

回答

0

试试这个

TextToSpeech textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { 
        @Override 
        public void onInit(int status) { 
          //your text 
          String textToSpeechStr = "Hello"; 

          //status is success/0 
          if (status == TextToSpeech.SUCCESS) { 
           //speech starts 

           textToSpeech.speak(textToSpeechStr, TextToSpeech.QUEUE_FLUSH, null); 
          } 

        } 
       }); 
+0

不,代码自动评论本身。无论如何,我可以只使用“tts.speak()”函数初始化api并转换多个句子。 –