2015-06-08 61 views
9

我嘗試在我的應用程序使用文字轉語音,文字轉語音:在API級別棄用說話功能21

String text = editText.getText().toString(); 
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 

但功能代言(字符串文本,詮釋queueMode,HashMap的PARAMS)在API級別21已被棄用。取而代之的是,建議使用speak(CharSequence文本,int queueMode,Bundle params,String utteranceId)。 但我不知道如何設置它。由於

回答

7
String text = editText.getText().toString(); 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    tts.speak(text,TextToSpeech.QUEUE_FLUSH,null,null); 
} else { 
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
} 
1

的回答是從https://stackoverflow.com/a/28000527/4211264

if (Build.VERSION.RELEASE.startsWith("5")) { 
    tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null, null); 
    } 
else { 
    tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null); 
    } 
0

採取下面是我

Private TextToSpeech ts 
ts=new TextToSpeech(CurrentActivity.this, new TextToSpeech.OnInitListener() { 
      @Override 
      public void onInit(int status) { 
       String text = "Any Text to Speak"; 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
        ts.speak(text,TextToSpeech.QUEUE_FLUSH,null,null); 
       } else { 
        ts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
       } 

      } 
     }); 
工作的全部工作
相關問題