我正在使用Android TTS播放文字 - android.speech.tts.TextToSpeech
我使用:TextToSpeech.speak
发言并.stop
停止发言。有没有办法暂停文本?
答案 0 :(得分:23)
TTS SDK没有我所知的任何暂停功能。但您可以使用synthesizeToFile()
创建包含TTS输出的音频文件。然后,您将使用MediaPlayer对象播放,暂停和停止播放该文件。根据文本字符串的长度,可能需要更长的时间来生成音频,因为synthesizeToFile()
函数必须在播放之前完成整个文件,但对于大多数应用程序来说,此延迟应该是可接受的
答案 1 :(得分:13)
我使用了分裂字符串并使用了playilence(),如下所示:
public void speakSpeech(String speech) {
HashMap<String, String> myHash = new HashMap<String, String>();
myHash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "done");
String[] splitspeech = speech.split("\\.");
for (int i = 0; i < splitspeech.length; i++) {
if (i == 0) { // Use for the first splited text to flush on audio stream
textToSpeech.speak(splitspeech[i].toString().trim(),TextToSpeech.QUEUE_FLUSH, myHash);
} else { // add the new test on previous then play the TTS
textToSpeech.speak(splitspeech[i].toString().trim(), TextToSpeech.QUEUE_ADD,myHash);
}
textToSpeech.playSilence(750, TextToSpeech.QUEUE_ADD, null);
}
}
答案 2 :(得分:6)
您可以通过添加最多三个句点(“。”)后跟单个空格“”来使TTS在句子之间或任何您想要的位置暂停。下面的示例在开头有一个很长的停顿,并且在消息正文之前有一个停顿。我不确定你到底是怎么回事。
private final BroadcastReceiver SMScatcher = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(
"android.provider.Telephony.SMS_RECEIVED")) {
// if(message starts with SMStretcher recognize BYTE)
StringBuilder sb = new StringBuilder();
/*
* The SMS-Messages are 'hiding' within the extras of the
* Intent.
*/
Bundle bundle = intent.getExtras();
if (bundle != null) {
/* Get all messages contained in the Intent */
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i < pdusObj.length; i++) {
messages[i] = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
}
/* Feed the StringBuilder with all Messages found. */
for (SmsMessage currentMessage : messages) {
// periods are to pause
sb.append("... Message From: ");
/* Sender-Number */
sb.append(currentMessage.getDisplayOriginatingAddress());
sb.append(".. ");
/* Actual Message-Content */
sb.append(currentMessage.getDisplayMessageBody());
}
// Toast.makeText(application, sb.toString(),
// Toast.LENGTH_LONG).show();
if (mTtsReady) {
try {
mTts.speak(sb.toString(), TextToSpeech.QUEUE_ADD,
null);
} catch (Exception e) {
Toast.makeText(application, "TTS Not ready",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
}
}
};
如果在最后一个句点之后省略空格,它将(或可能)不按预期工作。
答案 3 :(得分:4)
如果没有暂停选项,您可以在想要延迟TTS引擎发言的时间内添加静音。例如,这当然必须是预定的“暂停”,并且无助于包括暂停按钮的功能。
对于API&lt; 21:public int playSilence (long durationInMs, int queueMode, HashMap params)
对于&gt; 21:public int playSilentUtterance (long durationInMs, int queueMode, String utteranceId)
请务必使用TextToSpeech.QUEUE_ADD而不是TextToSpeech.QUEUE_FLUSH,否则会清除之前发表的演讲。
答案 4 :(得分:3)
我还没有尝试过这个,但我需要做同样的事情。我的想法是首先将你的演讲文本分成几个单词。
然后创建一个递归函数,在当前单词结束后播放下一个单词,同时保留当前单词的计数器。
答案 5 :(得分:2)
将messages
划分为多个部分并使用utterance
监听器
onutteranceprogress
tts.playSilence(1250, TextToSpeech.QUEUE_ADD, null);
答案 6 :(得分:2)
似乎如果你在一个单词后面加一个句子并用大写字母开始下一个单词,就像一个新句子,就像这样:
我们回家后我们吃晚餐。
&#34>家。我们&#34;然后会暂停一下。答案 7 :(得分:0)
此外,转义引号(\“)似乎也有点停顿 - 至少,如果你把它放在一个单词周围,它会在单词周围添加空格。
答案 8 :(得分:0)
此解决方案并不完美,但@Aaron C解决方案的替代方案可能是创建自定义文本到语音类,如下所示。如果您的文本相对较短并且每分钟的口语单词对于您正在使用的语言足够准确,则此解决方案可能会运行良好。
private class CustomTextToSpeech extends TextToSpeech {
private static final double WORDS_PER_MS = (double)190/60/1000;
long startTimestamp = 0;
long pauseTimestamp = 0;
private Handler handler;
private Runnable speakRunnable;
StringBuilder textToSpeechBuilder;
private boolean isPaused = false;
public CustomTextToSpeech(Context context, OnInitListener initListener){
super(context, initListener);
setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String arg0) {
Log.d(TAG, "tts done. " + arg0);
startTimestamp = 0;
pauseTimestamp = 0;
handler.postDelayed(speakRunnable, TTS_INTERVAL_MS);
}
@Override
public void onError(String arg0) {
Log.e(TAG, "tts error. " + arg0);
}
@Override
public void onStart(String arg0) {
Log.d(TAG, "tts start. " + arg0);
setStartTimestamp(System.currentTimeMillis());
}
});
handler = new Handler();
speakRunnable = new Runnable() {
@Override
public void run() {
speak();
}
};
textToSpeechBuilder = new StringBuilder(getResources().getString(R.string.talkback_tips));
}
public void setStartTimestamp(long timestamp) {
startTimestamp = timestamp;
}
public void setPauseTimestamp(long timestamp) {
pauseTimestamp = timestamp;
}
public boolean isPaused(){
return (startTimestamp > 0 && pauseTimestamp > 0);
}
public void resume(){
if(handler != null && isPaused){
if(startTimestamp > 0 && pauseTimestamp > 0){
handler.postDelayed(speakRunnable, TTS_SETUP_TIME_MS);
} else {
handler.postDelayed(speakRunnable, TTS_INTERVAL_MS);
}
}
isPaused = false;
}
public void pause(){
isPaused = true;
if (handler != null) {
handler.removeCallbacks(speakRunnable);
handler.removeMessages(1);
}
if(isSpeaking()){
setPauseTimestamp(System.currentTimeMillis());
}
stop();
}
public void utter(){
if(handler != null){
handler.postDelayed(speakRunnable, TTS_INTERVAL_MS);
}
}
public void speak(){
Log.d(TAG, "textToSpeechBuilder: " + textToSpeechBuilder.toString());
if(isPaused()){
String[] words = textToSpeechBuilder.toString().split(" ");
int wordsAlreadySpoken = (int)Math.round((pauseTimestamp - startTimestamp)*WORDS_PER_MS);
words = Arrays.copyOfRange(words, wordsAlreadySpoken-1, words.length);
textToSpeechBuilder = new StringBuilder();
for(String s : words){
textToSpeechBuilder.append(s);
textToSpeechBuilder.append(" ");
}
} else {
textToSpeechBuilder = new StringBuilder(getResources().getString(R.string.talkback_tips));
}
if (tts != null && languageAvailable)
speak(textToSpeechBuilder.toString(), TextToSpeech.QUEUE_FLUSH, new Bundle(), "utter");
}
}
答案 9 :(得分:0)
我使用了不同的方法。
科特琳代码:
class VoiceService {
private lateinit var textToSpeech: TextToSpeech
var sentenceCounter: Int = 0
var myList: List<String> = ArrayList()
fun resume() {
sentenceCounter -= 1
speakText()
}
fun pause() {
textToSpeech.stop()
}
fun stop() {
sentenceCounter = 0
textToSpeech.stop()
}
fun speakText() {
var myText = "This is some text to speak. This is more text to speak."
myList =myText.split(".")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.speak(myList[sentenceCounter], TextToSpeech.QUEUE_FLUSH, null, utteranceId)
sentenceCounter++
} else {
var map: HashMap<String, String> = LinkedHashMap<String, String>()
map[TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID] = utteranceId
textToSpeech.speak(myList[sentenceCounter], TextToSpeech.QUEUE_FLUSH, map)
sentenceCounter++
}
}
override fun onDone(p0: String?) {
if (sentenceCounter < myList.size) {
speakText()
} else {
speakNextText()
}
}
}