Search code examples
app-inventor

How to stop TextToSpeech from speaking the given message


In MIT App Inventor, there is a media component called TextToSpeech. It has a method called Speak(text message), which speaks a given message.

However it has no method to stop speaking. Imagine when the message is long, it can keep speaking for a long time, which is not desirable.

Wonder if there is a good way to ask TextToSpeech to stop speaking?


Solution

  • An efficient way to ask TextToSpeech to stop speaking is to call TextToSpeech's method Speak(text message) again, with an empty text. As a result, TextToSpeech will stop speaking immediately.

    Here is an example. Suppose that TextToSpeech1's Speak method has already been called to start speaking a long message. Now click Button1, and in its event handler you can call TextToSpeech1's method Speak(text message) with an empty text, as shown below. TextToSpeech1 will stop speaking right away.

    enter image description here

    Note on the App Inventor version I'm working with:

    Built: September 27 2019 Version: nb180
    Use Companion: 2.55 or 2.55u or 2.55 or 2.55uu
    Target Android SDK: 28 (Android 9.0 Pie)