Java word文档转MP3(纯文本)

<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文件,这个自行去百度,很多

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值