最近比较无聊,随便翻着博客,无心中看到了有的人用VBS读文本内容,也就是读几句中文,emmm,挺有趣的,实现也很简单,都不须要安装什么环境,直接新建txt文件,输入一些简单的vbs读文本的代码,而后将新建的文件后缀改成.vbs,而后双击一下就能够有效果了。。。。java
因而我就想啊,java行不行呢?查了一些资料,还真的行,我就将我试验的过程说一下,就看成娱乐娱乐!json
1.依赖api
随便新建一个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代码实现,运行以后就会读出来了;maven
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.输出音频文件编码
按理说到上面已经实现了功能,可是我还想着能不能把读的音频文件该输出一下呢?查了查资料,竟然还真行,代码以下:spa
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(); } } }
直接运行咱们就能够听到朗读的声音,并且在指定目录还能够找到音频文件;3d
4.调用百度AI来读文本
又按理说到上面应该就差很少了,可是我老是感受电脑自带的语音库声音很差听,我要用百度AI的那个比较可爱的声音,我仍是去查了查资料,竟然能够,并且很容易!
4.1.申请一下百度语音api权限
因为咱们是要去调用百度的api进行语音识别,那么咱们要先去申请一下权限,否则会一直报错(这个地方卡了很久,最后终于被我查出来为何报错了。。。),连接:http://ai.baidu.com/
而后会让你登陆一下,直接用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的其余功能也不错!