当前位置: 移动技术网 > IT编程>开发语言>Java > java简单实现用语音读txt文档方法总结

java简单实现用语音读txt文档方法总结

2019年07月19日  | 移动技术网IT编程  | 我要评论

彼得与狼教案,李洋子,锻件标准

最近比较无聊,随便翻着博客,无意中看到了有的人用vbs读文本内容,也就是读几句中文,emmm,挺有趣的,实现也很简单,都不需要安装什么环境,直接新建txt文件,输入一些简单的vbs读文本的代码,然后将新建的文件后缀改为.vbs,然后双击一下就可以有效果了。。。。

于是我就想啊,java行不行呢?查了一些资料,还真的行,我就将我试验的过程说一下,就当作娱乐娱乐!

1.依赖

随便新建一个maven项目,导入依赖

<dependency>
  <groupid>com.hynnet</groupid>
  <artifactid>jacob</artifactid>
  <version>1.18</version>
</dependency>

只导入依赖还不行,还要导入一个.dll文件,百度云链接:链接:https://pan.baidu.com/s/1yyypiopxrtuykebjzabhlw 提取码:s62o ,可以看到有两个dll文件,由于我的电脑是64位的,于是我将上面那个dll文件复制一份到当前使用jdk的bin目录下

2.java代码实现

一个很简单的java代码实现,运行之后就会读出来了;

package com.wyq.day66;

import com.jacob.activex.activexcomponent;
import com.jacob.com.dispatch;
import com.jacob.com.variant;

public class speak02 {
 
 //用电脑自带的语音读字符串str
 public static void main(string[] args) {
   string str = "你好,我是java小新人!请叫我最帅的帅锅";
   
   activexcomponent sap = new activexcomponent("sapi.spvoice");
   dispatch sapo = sap.getobject();
   try {
    // 音量 0-100
    sap.setproperty("volume", new variant(100));
    // 语音朗读速度 -10 到 +10
    sap.setproperty("rate", new variant(0));
    // 执行朗读 
    dispatch.call(sapo, "speak", new variant(str));
    
   } catch (exception e) {
    e.printstacktrace();
   } finally {
    sapo.saferelease();
    sap.saferelease();
   }
   
 }

}

3.输出音频文件

按理说到上面已经实现了功能,但是我还想着能不能把读的音频文件该输出一下呢?查了查资料,居然还真行,代码如下:

package com.wyq.day66;

import com.jacob.activex.activexcomponent;
import com.jacob.com.dispatch;
import com.jacob.com.variant;

public class javaspeak {

 public static void main(string[] args) {
  //指定文件音频输出文件位置
  string output = "e:\\test.wav";
  
  activexcomponent ax = null;
  string str="我是java小新人,我要将这段话的音频输出一下";
  try {
   ax = new activexcomponent("sapi.spvoice");

   //运行时输出语音内容
   dispatch spvoice = ax.getobject();
   // 音量 0-100
   ax.setproperty("volume", new variant(100));
   // 语音朗读速度 -10 到 +10
   ax.setproperty("rate", new variant(-3));
   // 进行朗读
   dispatch.call(spvoice, "speak", new variant(str));

   //下面是构建文件流把生成语音文件

   ax = new activexcomponent("sapi.spfilestream");
   dispatch spfilestream = ax.getobject();

   ax = new activexcomponent("sapi.spaudioformat");
   dispatch spaudioformat = ax.getobject();

   //设置音频流格式
   dispatch.put(spaudioformat, "type", new variant(22));
   //设置文件输出流格式
   dispatch.putref(spfilestream, "format", spaudioformat);
   //调用输出 文件流打开方法,在指定位置输出一个.wav文件
   dispatch.call(spfilestream, "open", new variant(output), new variant(3), new variant(true));
   //设置声音对象的音频输出流为输出文件对象
   dispatch.putref(spvoice, "audiooutputstream", spfilestream);
   //设置音量 0到100
   dispatch.put(spvoice, "volume", new variant(100));
   //设置朗读速度
   dispatch.put(spvoice, "rate", new variant(-2));
   //开始朗读
   dispatch.call(spvoice, "speak", new variant(str));

   //关闭输出文件
   dispatch.call(spfilestream, "close");
   dispatch.putref(spvoice, "audiooutputstream", null);

   spaudioformat.saferelease();
   spfilestream.saferelease();
   spvoice.saferelease();
   ax.saferelease();

   } catch (exception e) {
    e.printstacktrace();
   }
 
 }

}

直接运行我们就可以听到朗读的声音,而且在指定目录还可以找到音频文件;

4.调用百度ai来读文本

又按理说到上面应该就差不多了,但是我总是感觉电脑自带的语音库声音不好听,我要用百度ai的那个比较可爱的声音,我还是去查了查资料,居然可以,而且很容易!

4.1.申请一下百度语音api权限

由于我们是要去调用百度的api进行语音识别,那么我们要先去申请一下权限,不然会一直报错(这个地方卡了好久,最后终于被我查出来为什么报错了。。。),链接:

然后会让你登录一下,直接用qq登录就行;

创建完毕之后查看一下应用详情:

4.2.代码实现

做了这么多是操作就是为了得到这三个字符串,现在我们还要导入百度语音的依赖:

<!--百度语音播报sdk-->
 <dependency>
  <groupid>com.baidu.aip</groupid>
  <artifactid>java-sdk</artifactid>
  <version>4.4.1</version>
 </dependency>
 
 <!-- https://mvnrepository.com/artifact/org.json/json -->
 <dependency>
  <groupid>org.json</groupid>
  <artifactid>json</artifactid>
  <version>20160810</version>
 </dependency>

桌面上记事本中的内容:

java代码实现如下,其实就是利用百度ai读取我们计算机中的一个txt文档,输出mp3文件保存并到指定位置

package com.wyq.day66;

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.unsupportedencodingexception;
import java.util.hashmap;

import org.json.jsonobject;

import com.baidu.aip.speech.aipspeech;
import com.baidu.aip.speech.ttsresponse;
import com.baidu.aip.util.util;

public class speak03 {
 //设置appid/ak/sk,这三个参数是需要我们去百度ai平台申请的(也就是上面说的那三个字符串)
 public static final string app_id = "16447127";
 public static final string api_key = "8go31soiffr1oll5mpfkgtr9";
 public static final string secret_key = "jwsonglfzfrgsq30****noxz9zpjmbc";
 
 //readfile是我们的txt文档,writefile是输出的mp3格式
 public static string readfile = "c:\\users\\asus\\desktop\\says.txt";
 public static string writefile = "e:\\output.mp3";


 public static void main(string[] args) {
  //可以直接输入字符串也行,内容比较多的话还是用txt文档比较好一点
  //convertmp3("你好!我是百度ai智能,java小新人,很高兴和你见面,我们一定能成为很好的朋友的");
  
  
  //调用readtostring方法将一个txt文档中的数据读取出来变成一个字符串
  string string = readtostring(readfile);
  //将这个字符串用百度ai读一下输出mp3格式
  convertmp3(string);

 }
  public static void convertmp3(string str) {
   aipspeech client = new aipspeech(app_id, api_key, secret_key);
   // 可选:设置网络连接参数,就是超时时间
   client.setconnectiontimeoutinmillis(2000);
   client.setsockettimeoutinmillis(60000);

   // 设置一些可选参数
   hashmap<string, object> options = new hashmap<string, object>();
   options.put("spd", "5");//语速,取值0-9,默认为5中语速  非必选
   options.put("pit", "5");//音调,取值0-9,默认为5中语调  非必选
   options.put("per", "4");//发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女 非必选
   
   //百度ai开始读取传入的str字符串
   ttsresponse res = client.synthesis(str, "zh", 1, options);
   
   //服务器返回的内容,合成成功时为null,失败时包含error_no等信息
   jsonobject result = res.getresult(); 
   if (result != null) {
    system.out.printf("error:" + result.tostring()+"----------");
    return;
   }
   //生成的音频数据
   byte[] data = res.getdata();   
   jsonobject res1 = res.getresult();
   if (data != null) {
    try {
     //将生成的音频输出到指定位置
     util.writebytestofilesystem(data, writefile);
    } catch (ioexception e) {
     e.printstacktrace();
    }
   }
   
   if (res1 != null) {
    system.out.println(res1.tostring());
   }
  }
  
  //这个方法就是根据输入的文件路径,读取该文件内容返回一个很长的字符串,由于txt是gbk编码,所以我们变成字符串的时候也要用gbk
  //其实就是最基本的流操作
  public static string readtostring(string filename) { 
   string encoding = "gbk"; 
   file file = new file(filename); 
   long filelength = file.length(); 
   byte[] filecontent = new byte[filelength.intvalue()]; 
   
   try { 
    fileinputstream in = new fileinputstream(file); 
    in.read(filecontent); 
    in.close(); 
   } catch (filenotfoundexception e) { 
    e.printstacktrace(); 
   } catch (ioexception e) { 
    e.printstacktrace(); 
   } 
   
   try { 
    return new string(filecontent, encoding); 
   } catch (unsupportedencodingexception e) { 
    system.err.println("the os does not support " + encoding); 
    e.printstacktrace(); 
    return null; 
   } 
  }
 

}

输出的音频文件:

5.总结

感觉还是有点儿意思的,没事的时候用java玩一玩这些东西就当是打发时间!总是看一些框架原理啊什么的,时间长了也是比较无聊的,可以挖掘一下java的其他功能也不错!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网