(超简单)java 文本转音频

  1. 引入依赖
<dependency>
    <groupId>com.hynnet</groupId>
    <artifactId>jacob</artifactId>
    <version>1.18</version>
</dependency>

2.代码展示

/**
     * 文本转音频
     *
     * @param path   音频生成路径(全路径,带文件名)(例:D:\\aa\\a.mp3)
     * @param text   文本内容
     * @param volume 音量大小 0 - 100
     * @param speed  语音朗读速度 -10 到 +10
     * @return 是否成功
     */
    public static boolean text(String path, String text, int volume, int speed) {
        try {
            // 调用dll朗读方法
            ActiveXComponent ax = new ActiveXComponent("Sapi.SpVoice");
            // 音量 0 - 100
            ax.setProperty("Volume", new Variant(volume));
            // 语音朗读速度 -10 到 +10
            ax.setProperty("Rate", new Variant(speed));
            // 输入的语言内容
            Dispatch dispatch = ax.getObject();
            // 本地执行朗读
            Dispatch.call(dispatch, "Speak", new Variant(text));

            //开始生成语音文件,构建文件流
            ax = new ActiveXComponent("Sapi.SpFileStream");
            Dispatch sfFileStream = ax.getObject();
            //设置文件生成格式
            ax = new ActiveXComponent("Sapi.SpAudioFormat");
            Dispatch fileFormat = ax.getObject();

            // 设置音频流格式
            Dispatch.put(fileFormat, "Type", new Variant(22));
            // 设置文件输出流格式
            Dispatch.putRef(sfFileStream, "Format", fileFormat);
            // 调用输出文件流打开方法,创建一个音频文件
            Dispatch.call(sfFileStream, "Open", new Variant(path), new Variant(3), new Variant(true));
            // 设置声音对应输出流为输出文件对象
            Dispatch.putRef(dispatch, "AudioOutputStream", sfFileStream);
            // 设置音量
            Dispatch.put(dispatch, "Volume", new Variant(volume));
            // 设置速度
            Dispatch.put(dispatch, "Rate", new Variant(speed));
            // 执行朗读
            Dispatch.call(dispatch, "Speak", new Variant(text));
            // 关闭输出文件
            Dispatch.call(sfFileStream, "Close");
            Dispatch.putRef(dispatch, "AudioOutputStream", null);

            // 关闭资源
            sfFileStream.safeRelease();
            fileFormat.safeRelease();
            // 关闭朗读的操作
            dispatch.safeRelease();
            ax.safeRelease();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

3.若报错

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jacob-1.18-x64 in java.library.path
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
	at java.lang.Runtime.loadLibrary0(Runtime.java:870)
	at java.lang.System.loadLibrary(System.java:1122)
	at com.jacob.com.LibraryLoader.loadJacobLibrary(LibraryLoader.java:184)
	at com.jacob.com.JacobObject.<clinit>(JacobObject.java:110)
	at com.ymw.common.utils.TextToAudioUtils.text(TextToAudioUtils.java:33)
	at com.ymw.common.utils.TextToAudioUtils.main(TextToAudioUtils.java:82)

将以下文件放入jdk的bin目录下即可
jacob-1.18-x64.dll 文件下载(免费)

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Java可以通过使用第三方库来实现文本音频。以下是一个使用FreeTTS库的例子: 1. 首先需要下载FreeTTS库并将其添加到Java项目中。 2. 在Java代码中,可以使用以下代码实现文本音频: ``` import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioInputStream; import com.sun.speech.freetts.Voice; import com.sun.speech.freetts.VoiceManager; public class TextToSpeech { public static void main(String[] args) throws Exception { // 设置要换的文本 String text = "你好,这是一个测试!"; // 初始化VoiceManager VoiceManager voiceManager = VoiceManager.getInstance(); // 选择要使用的音频引擎 Voice voice = voiceManager.getVoice("kevin16"); // 分配音频引擎资源 voice.allocate(); // 创建一个音频流,将换后的音频输出到其中 AudioInputStream audioStream = AudioSystem.getAudioInputStream(voice.synthesize(text)); // 保存音频文件 AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE, new File("output.wav")); // 释放音频引擎资源 voice.deallocate(); } } ``` 在上面的代码中,我们首先设置要换的文本,然后选择使用的音频引擎。在本例中,我们使用了Kevin16音频引擎。然后,我们分配音频引擎资源并创建一个音频流,将换后的音频输出到其中。最后,我们将音频文件保存到本地,并释放音频引擎资源。 请注意,这个例子只是一个简单的演示,需要根据具体的需求进行修改和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值