android - TextToSpeech 在 Android 中使用 synthesizeToFile 需要太多时间

标签 android text-to-speech speech-synthesis google-text-to-speech

我使用下面的代码使用 Android 内置将 .txt 文件合成为 .mp3 文件TTS 引擎

代码:

 textToSpeech.synthesizeToFile(readFileText, utterParam, destinationFileName);

 textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onStart(final String utteranceId) {
                    Log.e(TAG, "onStart...");
                }

                @Override
                public void onDone(final String utteranceId) {
                    Log.e(TAG, "onDone...");
                }

                @Override
                public void onError(String utteranceId) {
                    Log.e(TAG, "onError...");
                }
            });

上面是示例代码。 这是应用程序执行的流程:

  1. 从SD卡中获取文件
  2. 将文件合成为mp3
  3. 播放 mp3 文件

问题: 当文件合成完成后,只有我可以播放 mp3 文件。即使是 1 MB 大小的文件也需要大约 1 分钟。

有什么我可以做的改进吗?

注意:我们需要使用 MediaPlayer 因为我们需要播放/暂停阅读器。

谢谢。

最佳答案

我已经解决了这个问题,将整个文件转换成段落 block 并将段落添加到 TTS 引擎中并直接播放。

 public static String[] convertFileToParagraph(String fileContent) {

//        String pattern = "(?<=(rn|r|n))([ \t]*$)+";
        String pattern = "([ \\t\\r]*\\n[ \\t\\r]*)+";
        return Pattern.compile(pattern, Pattern.MULTILINE).split(fileContent);
    }

/**
     * Divides files in to paragraphs
     */
    private void divideFileToChunks() {
        try {
            currentFileChunks = convertFileToParagraph(fileContent);
            currentFileChunks = makeSmallChunks(currentFileChunks);
            addChunksToTTS();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Divides file paragraphs into sentences of 200 characters
     *
     * @param currentFileChunks : list of paragraphs
     * @return : list of divided file
     */
    private String[] makeSmallChunks(String[] currentFileChunks) {
        try {
            ArrayList<String> smallChunks = new ArrayList<>();
            for (int i = 0; i < currentFileChunks.length; i++) {
                String chunk = currentFileChunks[i];
                if (chunk != null && chunk.length() > 200) {
                    int length = chunk.length();
                    int count = length / 200;
                    int modulo = length % 200;
                    for (int j = 0; j < count; j++) {
                        smallChunks.add(chunk.substring(200 * j, (200 * j) + 199));
                    }
                    if (modulo > 0) {
                        smallChunks.add(chunk.substring(chunk.length() - 1 - modulo, chunk.length() - 1));
                    }
                } else {
                    smallChunks.add(chunk);
                }
            }
            return smallChunks.toArray(new String[smallChunks.size()]);
        } catch (Exception e) {
            e.printStackTrace();
            return currentFileChunks;
        }
    }

    /**
     * Add all chunks to TTS(Text to Speech) Engine
     */
    private void addChunksToTTS() {
        try {
            String[] chunks = getCurrentFileChunks();
            if (chunks != null && chunks.length > 0) {
                for (int i = currentChunk; i < chunks.length; i++) {
                    utterParam.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.valueOf(i));
                    textToSpeech.speak(chunks[i], TextToSpeech.QUEUE_ADD, utterParam);
                    imgBtnT2SPlay.setImageResource(R.drawable.icon_pause_white);
                    edtT2SFileContents.setEnabled(false);
                    isPlaying = true;
                }
            }

            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

谢谢。

关于android - TextToSpeech 在 Android 中使用 synthesizeToFile 需要太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36999264/

相关文章:

android - 在 Realm for Android 中直接访问字段

android - 无法解析 React Native 中配置 ':app:debugCompileClasspath' 的所有文件

android - 使用一个启动器在分屏中启动我的两个应用程序 - Android

android - 如何在文本转语音中使用印地语(印度母语)?

javascript - 尝试在iOS webview应用程序中使用speechSynthesis API

android - 了解 Android 中的 Looper 是什么

api - 您知道评估英语发音的API服务吗?

c# - 将 NAudio 与 SpeechSynthesizer 结合使用

objective-c - 如何查找 NSSpeechSynthesizer 的 'rate' 属性的范围?

c# - 监听特定语法c#