Java 文字转语音(TTS)

如何将文字转成语音呢?本文是使用windows自带的语音引擎去实现的。所以需要下载一个jar包。

准备工作:

  1. 1、jacob-1.17-M2.rar在百度上下载http://download.csdn.net/detail/zzl819748566/9918533
  2. 2、解压jacob-1.17-M2.rar
  3. 3、向工程里导入jacob.jar
  4. 4、将jacob-1.17-M2-x64.dll拷贝到jdk的bin目录和windows/system32目录

104701_wmrc_2431191.png

104718_KYts_2431191.png

public static void main(String[] args) throws IOException {
        //
        ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
        // Dispatch是做什么的?
        Dispatch sapo = sap.getObject();
        try {

            // 音量 0-100
            sap.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            sap.setProperty("Rate", new Variant(-2));

            Variant defalutVoice = sap.getProperty("Voice");

            Dispatch dispdefaultVoice = defalutVoice.toDispatch();
            Variant allVoices = Dispatch.call(sapo, "GetVoices");
            Dispatch dispVoices = allVoices.toDispatch();

            Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch();
            ActiveXComponent voiceActivex = new ActiveXComponent(dispdefaultVoice);
            ActiveXComponent setvoiceActivex = new ActiveXComponent(setvoice);

            Variant item = Dispatch.call(setvoiceActivex, "GetDescription");
            // 执行朗读
            Dispatch.call(sapo, "Speak", new Variant("嗨,小明,早上好"));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sapo.safeRelease();
            sap.safeRelease();
        }
    }

总结:目前只试过在windows上试过,linux环境下还未用过(未完待续)。其实我是想把文字放到歌曲中的唱出来的,目前还没有一个好的思路。

转载于:https://my.oschina.net/alarm1673/blog/1499335

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值