Zain Ul Abidin
Zain Ul Abidin

Reputation: 2700

Control Rate of Android Text to Speech with respect to time

Hello for some reasons I need to control the TTS rate factor according to a time value for example I know how to setup a simple TTS e.g. the following code

TextToSpeech tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("Wow I am working", TextToSpeech.QUEUE_ADD, null);

and to control the text speech rate we can use setSpeechRate(value) e.g.

tts.setSpeechRate(2.0) //doubles the normal speed

but I am not able to impliment a way so that the rate should be calculated according to a time factor let's say 10 seconds so the phrase should be completed in speech in specified time.

Upvotes: 0

Views: 1047

Answers (1)

D. Kupra
D. Kupra

Reputation: 373

If those are your only specifications, you could use tts.synthesizeToFile() and save the output somewhere inside filesdir, then use MediaMetadataRetriever to tell you how long it is at speed scale 1. Then just divide the duration by how long you want it to be and that's your new scale.

Edit:

For creating the synthesized file:

val sentence="this is sample text"
val f = File(filesDir, "ttstemprecord")
val b = Bundle()
tts.synthesizeToFile(sentence, b, f, "")

For getting the duration:

val soundPath=getFilesDir().absolutePath + "ttstemprecord"
val mmr= MediaMetadataRetriever()
mmr.setDataSource(soundPath)  dur=Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION))

For the speed:

val speed=dur/durationThatIWant

Upvotes: 1

Related Questions