5

我用下面的代码使用Android内置TTS Engine合成.txt文件.mp3文件。文字转语音花费太多时间,而synthesizeToFile Android中

代码:

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

问题上发挥的MP3文件:当文件综合化完成后则只我可以播放mp3文件。对于大小为1 MB的文件,大约需要1分钟。

我能做些什么改进吗?

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

谢谢。

+0

可能是有用的链接[android-sdk-using-the-text-to-speech-engine](http://code.tutsplus.com/tutorials/android-sdk-using-the-text-to-speech -engine - mobile-8540) –

+1

综合比说话更快吗?如果是,那么为什么不合成并以较小的块回放?然后创建第一个MP3文件,并准备好更快地播放,如果其他块可以在后台快速处理,那么当前一个块被播放时,它们将始终准备好等待播放。 –

+0

由于需要暂停播放的功能,是否需要在“说出”发音之前进行综合的唯一原因?你试图合成多少个字符?发动机有多少他们可以接受的限制 - 每个发动机都不相同。输出是wav/pcm不是mp3 - 你是通过一些转换来运行这个还是这是你如何标记文件的错误? – brandall

回答

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

感谢。

+0

也请添加getCurrentFileChunks()方法 – Ragini