SpeechToText synthesizeToFile 不排队

SpeechToText synthesizeToFile not queuing

我正在尝试使用 TextToSpeech.synthesizeToFile 方法将一些演讲内容写入 mp3。文本超过 4000 个字符,因此我不得不将其分成块。问题是只有最后一段文本最终成为音频文件,该方法似乎是覆盖文件而不是添加文件。

int maxLength = 1000; // TextToSpeech.getMaxSpeechInputLength() - 1; smaller chunks = start streaming sooner?
String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test_p.mp3";
File file = new File(fileName);
final String utteranceId = "fooo";
for (int i = 0; i < elementListJson.length(); i++) {
    JSONArray elArray = elementListJson.getJSONArray(i);
    nodeType = elArray.get(0).toString();
    nodeText = elArray.get(1).toString();
    ArrayList<String> chunkedNodeText = StringHelper.splitToLineLengthWithoutBreakingWords(nodeText, maxLength);
    for(String chunk : chunkedNodeText) {
        Log.d("TTSW", "p"+partsProcessed+"   = "+chunk);
        int speechStatus = textToSpeech.synthesizeToFile(
                chunk
                , null, file, utteranceId);
        if (speechStatus == TextToSpeech.ERROR) {
            showNotificationHint("Error converting text to speech! #1");
        }
    }
}

此代码适用于 speak() 方法和 QUEUE 参数,但我需要将其写入单个文件以允许用户 pause/rewind/fast 转发控制。我找不到告诉 synthesizeToFile 排队的参数,但根据文档注释,它无论如何都应该排队工作。

我试了一下,但它有点像潘多拉魔盒!

似乎无法使用 synthesizeToFile() "append" 对现有文件的新语句 -- 它总是会重写文件。

看来完成您想要做的事情的唯一方法是:

A) 写入所有单独的文件(文件 1、文件 2、文件 3 等),然后在*它们全部创建完成后按顺序合并它们。

B) 使用循环为每个连续的话语写入 "temporary" 文件,每次迭代将临时文件合并到 "main" 文件。但是,由于 sythesizeToFile() 是异步的,您需要 pause/control 使用 UtteranceProgressListener 来控制此循环的流程,以防止临时文件被过早覆盖。

其他注意事项:

1) synthesizeToFile() 只会生成 WAV,即使您将其命名为 mp3。

2) * - synthesizeToFile() 是异步的,因此您将不可避免地需要使用 UtteranceProgressListener 来防止过早覆盖。

3) WAV 文件很大,因此您需要清理它们。