我使用下面的代码使用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...");
}
});
以上是示例代码.
以下是应用程序执行流程:
问题:文件合成完成后,我只能播放mp3文件.对于大小为1 mb的文件,大约需要1分钟.
我可以做些什么改进吗?
注意:我们需要使用MediaPlayer,因为我们需要播放/暂停阅读器.
谢谢.
最佳答案
我已经解决了这个问题,将整个文件转换成段的段落,并将段落添加到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 {
ArrayListprintstacktrace();
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();
}
}
谢谢.
原文地址:https://www.jb51.cc/android/429983.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。