在Android中,使用synthesizeToFile时TextToSpeech需要太长时间

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

代码:

 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文件。对于1MB大小的文件,需要大约1分钟的时间。
我是否可以有所改进? 注意:我们需要使用MediaPlayer,因为我们需要播放/暂停读者。
谢谢。

可能有用的链接 android-sdk-using-the-text-to-speech-engine - Pratik Butani
1
合成比说话更快吗?如果是,为什么不将其合成并分成较小的块进行播放?第一个MP3文件将更快地创建并准备好播放,如果其他块可以在后台快速处理,那么它们将始终准备好等待前一个块播放完毕后进行播放。 - Markus Kauppinen
你需要在“发言”之前合成语音的唯一原因是需要暂停播放的能力吗?你尝试合成多少个字符?引擎对其可以接受的数量有限制-这因引擎而异。输出为wav / pcm而不是mp3-您是否将其通过某种转换运行,还是标记文件时出现了错误? - brandall
1个回答

3
我已经解决了将整个文件转换为段落块,并将段落添加到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();
        }
    }

感谢您的选择。

请添加getCurrentFileChunks()方法。 - Ragini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接