[英]How to add line breaks on TextToSpeech android?
我正在使用Android中的TextToSpeech
讀取文本。 在我的情況下,我有4個字符串可以讀取,字符串之間的延遲很小。 我在字符串的末尾添加了句號。 但是它讀為dot
。 這是我嘗試過的
public class TextToSpeechUtil implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {
private Activity activity;
private String message;
private TextToSpeech tts;
public TextToSpeechUtil(Activity activity, String message) {
this.activity = activity;
this.message = message;
tts = new TextToSpeech(activity, this);
tts.setLanguage(Locale.US);
}
public void shutdown() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}
tts.setOnUtteranceCompletedListener(this);
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
HashMap<String, String> hashAlarm = new HashMap<>();
hashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, message);
tts.speak(message, TextToSpeech.QUEUE_FLUSH, hashAlarm);
}
@Override
public void onUtteranceCompleted(String utteranceId) {
shutdown();
}
}
訊息字串
speechUtil = new TextToSpeechUtil(this, "String 1."
.concat("String 2.")
.concat("String 3.")
.concat("String 4.")
.concat("String 5."));
這些字符串正在連續讀取。 我該如何解決?
static Timer timing;
while (st.hasMoreTokens()) {
tts.speak(st.nextToken()//(or) your string, TextToSpeech.QUEUE_ADD, null);
}
if (timing != null) {
timing.cancel();
timing = null;
}
String strRepeat="Repeat";
timing = new Timer();
timing.schedule(new Updater(con,txt_title,strRepeat), 20000,20000);
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
check_ttsComplete=status;
int result = tts.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Language not supported", Toast.LENGTH_LONG).show();
Log.e("TTS", "Language is not supported");
} else {
// btnSpeak.setEnabled(true);
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
@Override
public void onUtteranceCompleted(String uttId) {
Toast.makeText(Activity.this,"done", Toast.LENGTH_LONG).show();
if (uttId.equalsIgnoreCase("done")) {
Toast.makeText(Activity.this,"inside done", Toast.LENGTH_LONG).show();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.