5

我已经使用下面的代码来合成.txt文件到.mp3使用 Android 内置的文件TTS Engine

代码:

 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我们需要播放/暂停阅读器。

谢谢。

4

1 回答 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();
        }
    }

谢谢。

于 2016-05-31T10:01:36.557 回答