By oflisback

2018-05-29 14:29:19 8 Comments

My goal is to use the Android Text-to-speech API play a sentence of text as speech while tracking the currently spoken word.

To get a smooth natural playback I use:

tts.speak("This is the sentence", TextToSpeech.QUEUE_FLUSH, null, null)

but then I can't keep track of the currently spoken word.

To play the sentence while keeping track of the currently spoken word I use:

val words = "This is the sentence".split(" ")
words.forEachIndexed { index, element ->
    tts.speak(element, TextToSpeech.QUEUE_ADD, null, index.toString())

combined with an UtteranceProgressListener, but then the speech is very jerky and not read back as a natural sentence.

Is there a way to get both a naturally spoken sentence and at the same time keep track of the currently spoken word?


@Richard Sahlin 2018-05-30 08:14:20

If you look in the latest Android documentation you will notice a new method introduced to the UtteranceProgressListener in API level 26 - called onRangeStart((String utteranceId, int start, int end, int frame),%20int,%20int,%20int)

However, as the documentation states:

"Only called if the engine supplies timing information by calling rangeStart(int, int, int)"

This is implemented in the SynthesisCallback:,%20int,%20int)

Again documentation states:

"The service may call this method to provide timing information about the spoken text."

So, sadly this means that the callback providing the timing information you need is implementation dependant.

On my device running Android 8.0.0 and using the default tts engine ( I did not get the callback.

In order to test you will need to

  • to build against SDK level 26 or above
  • Implement your own UtteranceProgressListener
  • Set it to the TextToSpeech engine by calling TextToSpeech.setOnUtteranceProgressListener(listener);
  • Override the onRangeStart(String, int, int, int) method in your UtteranceProgressListener.

If your implementation supports the timing information you will the callback, if not perhaps your best bet is to find another engine implementation or perhaps implement your own TextToSpeechService.

Related Questions

Sponsored Content

31 Answered Questions

[SOLVED] How to get the build/version number of your Android application?

40 Answered Questions

[SOLVED] How to get screen dimensions as pixels in Android

40 Answered Questions

[SOLVED] How to get current time and date in Android

  • 2011-03-20 16:12:25
  • M7M
  • 1434758 View
  • 1121 Score
  • 40 Answer
  • Tags:   android date time

65 Answered Questions

[SOLVED] How do I center text horizontally and vertically in a TextView?

  • 2009-01-11 00:27:55
  • pupeno
  • 1253775 View
  • 2049 Score
  • 65 Answer
  • Tags:   android textview

3 Answered Questions

2 Answered Questions

[SOLVED] Text To Speech With Custom Array-Adapter Android

1 Answered Questions

[SOLVED] Android Speech to Text - offline constant recognition

  • 2013-01-12 03:23:13
  • jax
  • 1159 View
  • 2 Score
  • 1 Answer
  • Tags:   android

Sponsored Content