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