Android: until tts talk is complete, then continue

I'm really struggling with something ... I have a couple of sentences that I want to read, both orally through the tes speek function, and through the text on the screen, one sentence at a time.

I have a text box area, but composing is something that I don’t get. Either he will read all the sentences and show only the last, or he will show and read only the first sentence.

Does anyone know how I can achieve this?

+4
source share
4 answers

I just ran into this problem, according to the method, use the UtteranceProgressListener . I found out that this is not running in the user interface thread, so I had to use runOnUiThread () to get back to updating the activity.

tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Override public void onStart(String utteranceId) { } @Override public void onDone(String utteranceId) { LettersActivity.this.runOnUiThread(new Runnable() { @Override public void run() { // Do something on UI thread } }); } @Override public void onError(String utteranceId) { Log.e(TAG, "error on " + utteranceId); } }); 
+3
source
 public void speak(String message){ tts.speak(message, TextToSpeech.QUEUE_FLUSH, null); while (tts.isSpeaking()){ System.Out.Println("Do something or nothing while speaking..") } } 
+1
source

try it

 public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{ private boolean initialized; private String queuedText; private String TAG = "TTS"; private TextToSpeech tts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tts = new TextToSpeech(this /* context */, this /* listener */); tts.setOnUtteranceProgressListener(mProgressListener); speak("hello world"); } public void speak(String text) { if (!initialized) { queuedText = text; return; } queuedText = null; setTtsListener(); // no longer creates a new UtteranceProgressListener each time HashMap<String, String> map = new HashMap<String, String>(); map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId"); tts.speak(text, TextToSpeech.QUEUE_ADD, map); } private void setTtsListener() { } @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { initialized = true; tts.setLanguage(Locale.ENGLISH); if (queuedText != null) { speak(queuedText); } } } private abstract class runnable implements Runnable { } private UtteranceProgressListener mProgressListener = new UtteranceProgressListener() { @Override public void onStart(String utteranceId) { } // Do nothing @Override public void onError(String utteranceId) { } // Do nothing. @Override public void onDone(String utteranceId) { new Thread() { public void run() { MainActivity.this.runOnUiThread(new runnable() { public void run() { Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show(); } }); } }.start(); } }; } 
0
source
 boolean speakingEnd = tts.isSpeaking(); do{ speakingEnd = tts.isSpeaking(); } while (speakingEnd); //Continue with code 
-1
source

Source: https://habr.com/ru/post/1336902/


All Articles