[英]Android static TextToSpeech
我的問題是我有一個Android應用程序,它應該告訴用戶一些信息。 我想吃點東西
Speaker.say("Hello World!");
// Wait till sentence is said
Speaker.say("Its " + time);
// Wait again
NextCommand.xyz();
我試過了
import android.speech.tts.TextToSpeech;
public class TTS implements TextToSpeech.OnInitListener {
private static TextToSpeech mTts;
private String text;
private static final TTS helper = new TTS();
public static TTS getInstance(){
return helper;
}
public void say(String text){
if(mTts == null){
this.text = text;
mTts = new TextToSpeech(context /* ignore this please */, helper);
mTts.setPitch(3);
}
else{
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
public void stopTTS(){
if(mTts != null){
mTts.shutdown();
mTts.stop();
mTts = null;
}
}
}
然后在onCreate中:
TTS tts = TTS.getInstance();
tts.say("Hello World");
tts.say("Hello again!")
但是:只說第一句話, optionsMenu
創建optionsMenu
這個怎么做?
我解決了:
import android.speech.tts.TextToSpeech;
import java.util.Locale;
public class Test implements TextToSpeech.OnInitListener {
TextToSpeech tts;
String text;
public Test(String text) {
tts = new TextToSpeech(MyApplication.getContext(), this);
this.text = text;
}
@Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.GERMAN);
tts.setPitch(3);
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
while(tts.isSpeaking());
}
}
}
然后您將其稱為:
new Test("Hello");
new Test("Hello again!");
Toast.makeText(this, "Finished", Toast.LENGTH_LONG).show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.