Android Text to Speech 相对于时间的控制率

Control Rate of Android Text to Speech with respect to time

你好,出于某些原因,我需要根据时间值控制 TTS 速率因子,例如,我知道如何设置简单的 TTS,例如以下代码

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

为了控制文本语速,我们可以使用 setSpeechRate(value) 例如

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

但我无法实施一种方法,以便根据时间因素计算速率,比方说 10 秒,这样短语应该在指定时间内在语音中完成。

如果这些是您唯一的规格,您可以使用 tts.synthesizeToFile() 并将输出保存在 filesdir 中的某处,然后使用 MediaMetadataRetriever 告诉您它在速度标度 1 下的时长。然后将持续时间除以按您希望的时间长度,这就是您的新比例。

编辑:

创建合成文件:

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

获取时长:

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

对于速度:

val speed=dur/durationThatIWant