<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
所需要的pom文件
具体代码
package com.review.convert;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
/**
* @author wanghonglin
*/
public class ConvertMP3 {
/**
* 文本转音频
*
* @param path 音频生成路径(全路径,带文件名)(例:D:\\aa\\a.mp3)
* @param filePath 文本内容
* @param volume 音量大小 0 - 100
* @param speed 语音朗读速度 -10 到 +10
* @return 是否成功
*/
public void text(String path, String filePath, int volume, int speed) {
ActiveXComponent ax = null;
Dispatch spFileStream = null;
Dispatch spAudioFormat = null;
Dispatch spVoice = null;
try{
String text = getTextFromDocument(filePath);
ax = new ActiveXComponent("Sapi.SpFileStream");
spFileStream = ax.getObject();
ax = new ActiveXComponent("Sapi.SpAudioFormat");
spAudioFormat = ax.getObject();
spVoice = new ActiveXComponent("Sapi.SpVoice").getObject();
// 设置音频流格式
Dispatch.put(spAudioFormat, "Type", new Variant(22));
// 设置文件输出流格式
Dispatch.putRef(spFileStream, "Format", spAudioFormat);
// 调用输出 文件流打开方法,创建一个.wav文件
Dispatch.call(spFileStream, "Open", new Variant(path), new Variant(3), new Variant(true));
// 设置声音对象的音频输出流为输出文件对象
Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
// 设置音量 0 ~ 100
Dispatch.put(spVoice, "Volume", new Variant(volume));
// 设置朗读速度 -10 ~ +10
Dispatch.put(spVoice, "Rate", new Variant(speed));
Dispatch.call(spVoice, "Speak", new Variant(text));
System.out.println("输出语音文件成功!");
}catch(Exception e){
e.printStackTrace();
}finally {
// 关闭输出文件
Dispatch.call(Objects.requireNonNull(spFileStream), "Close");
Dispatch.putRef(Objects.requireNonNull(spVoice), "AudioOutputStream", null);
Objects.requireNonNull(spAudioFormat).safeRelease();
spFileStream.safeRelease();
spVoice.safeRelease();
ax.safeRelease();
}
}
/**
* 读取文件,获取文本
* @param filePath 文件路径
* @return
* @throws IOException
*/
public String getTextFromDocument(String filePath) throws IOException {
//加载Word文档
FileInputStream fis = new FileInputStream(filePath);
XWPFDocument document = new XWPFDocument(fis);
//获取文本内容
StringBuilder sb = new StringBuilder();
//遍历内容
for (XWPFParagraph paragraph : document.getParagraphs()){
//遍历段落中的所有文本
for (XWPFRun run : paragraph.getRuns()){
sb.append(run.getText(0));
}
}
return sb.toString();
}
}
public static void main(String[] args) {
ConvertMP3 convertMP3 = new ConvertMP3();
convertMP3.text("D:\\a\\a\\a.mp3","D:\\a\\a\\a.docx",100,5);
}
为了方便word文件转换成MP3听,音量最好设置最大,可以自行调节
可以通过下面的代码试听
// 本地执行朗读
Dispatch.call(dispatch, "Speak", new Variant(text));
还需要一个jacob-1.18-x64.dll文件,这个自行去百度,很多