利用百度语音识别技术实现文字转语音的应用(Java版附源码)
一、基本介绍
语音交互的需求场景越来越多,很多开发者都想做一款把语音转换成文字的游戏或者应用,这需要用到语音识别技术" title="语音识别技术">语音识别技术,这里会对如何使用完全免费的百度语音开放平台来快速开发,拥有高大上的语音识别技术的应用。
二、引入jar包
下载jacob-1.18.zip
并导入jacob.jar、json-20160810.jar、log4j-1.2.17.jar
将解压后的文件中jacob-1.18-x64.dll复制到对应的JDK中(我的是C:Program FilesJavajdk1.8.0_152jrein)
三、代码实例
1、工具类,ConnUtil
package baidu.restapi.common;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
/**
* 与连接相关的Util类
*/
public class ConnUtil {
/**
* UrlEncode, UTF-8 编码
*
* @param str 原始字符串
* @return
*/
public static String urlEncode(String str) {
String result = null;
try {
result = URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
/**
* 从HttpURLConnection 获取返回的字符串
*
* @param conn
* @return
* @throws IOException
* @throws DemoException
*/
public static String getResponseString(HttpURLConnection conn) throws IOException, DemoException {
return new String(getResponseBytes(conn));
}
/**
* 从HttpURLConnection 获取返回的bytes
* 注意 HttpURLConnection自身问题, 400类错误,会直接抛出异常。不能获取conn.getInputStream();
*
* @param conn
* @return
* @throws IOException http请求错误
* @throws DemoException http 的状态码不是 200
*/
public static byte[] getResponseBytes(HttpURLConnection conn) throws IOException, DemoException {
int responseCode = conn.getResponseCode();
if (responseCode != 200) {
System.err.println("http 请求返回的状态码错误,期望200, 当前是 " + responseCode);
if (responseCode == 401) {
System.err.println("可能是appkey appSecret 填错");
}
throw new DemoException("http response code is" + responseCode);
}
InputStream inputStream = conn.getInputStream();
byte[] result = getInputStreamContent(inputStream);
return result;
}
/**
* 将InputStream内的内容全部读取,作为bytes返回
*
* @param is
* @return
* @throws IOException @see InputStream.read()
*/
public static byte[] getInputStreamContent(InputStream is) throws IOException {
byte[] b = new byte[1024];
// 定义一个输出流存储接收到的数据
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 开始接收数据
int len = 0;
while (true) {
len = is.read(b);
if (len == -1) {
// 数据读完
break;
}
byteArrayOutputStream.write(b, 0, len);
}
return byteArrayOutputStream.toByteArray();
}
}
2、token的获取类,TokenHolder
package baidu.restapi.common;import org.json.JSONObject;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* token的获取类
* 将apiKey和secretKey换取token,注意有效期保存在expiresAt
*/
public class TokenHolder {
public static final String ASR_SCOPE = "audio_voice_assistant_get";
public static final String TTS_SCOPE = "audio_tts_post";
/**
* url , Token的url,http可以改为https
*/
private static final String url = "http://openapi.baidu.com/oauth/2.0/token";
/**
* asr的权限 scope 是 "audio_voice_assistant_get"
* tts 的权限 scope 是 "audio_tts_post"
*/
private String scope;
/**
* 网页上申请语音识别应用获取的apiKey
*/
private String apiKey;
/**
* 网页上申请语音识别应用获取的secretKey
*/
private String secretKey;
/**
* 保存访问接口获取的token
*/
private String token;
/**
* 当前的时间戳,毫秒
*/
private long expiresAt;
/**
* @param apiKey 网页上申请语音识别应用获取的apiKey
* @param secretKey 网页上申请语音识别应用获取的secretKey
*/
public TokenHolder(String apiKey, String secretKey, String scope) {
this.apiKey = apiKey;
this.secretKey = secretKey;
this.scope = scope;
}
/**
* 获取token,refresh 方法后调用有效
*
* @return
*/
public String getToken() {
return token;
}
/**
* 获取过期时间,refresh 方法后调用有效
*
* @return
*/
public long getExpiresAt() {
return expiresAt;
}
/**
* 获取token
*
* @return
* @throws IOException http请求错误
* @throws DemoException http接口返回不是 200, access_token未获取
*/
public void resfresh() throws Exception {
String getTokenURL = url + "?grant_type=client_credentials"
+ "&client_id=" + ConnUtil.urlEncode(apiKey) + "&client_secret=" + ConnUtil.urlEncode(secretKey);
// 打印的url出来放到浏览器内可以复现
System.out.println("token url:" + getTokenURL);
URL url = new URL(getTokenURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
String result = ConnUtil.getResponseString(conn);
System.out.println("Token result json:" + result);
parseJson(result);
}
/**
* @param result token接口获得的result
* @throws DemoException
*/
private void parseJson(String result) throws Exception {
JSONObject json = new JSONObject(result);
if (!json.has("access_token")) {
// 返回没有access_token字段
throw new DemoException("access_token not obtained, " + result);
}
if (!json.has("scope")) {
// 返回没有scope字段
throw new DemoException("scopenot obtained, " + result);
}
if (!json.getString("scope").contains(scope)) {
throw new DemoException("scope not exist, " + scope + "," + result);
}
token = json.getString("access_token");
expiresAt = System.currentTimeMillis() + json.getLong("expires_in") * 1000;
}
}
3、异常类,DemoException
package baidu.restapi.common;public class DemoException extends Exception {
public DemoException(String message) {
super(message);
}
}
4、测试类,TtsMain
package baidu.restapi.ttsdemo;import baidu.restapi.common.ConnUtil;
import baidu.restapi.common.TokenHolder;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TtsMain {
public static void main(String[] args) throws Exception {
(new TtsMain()).run();
}
// 填写网页上申请的appkey 如 $apiKey="g8eBUMSokVB1BHGmgxxxxxx"
private final String appKey = "4E1BG9lTnlSeIf1NQFlrSq6h";
// 填写网页上申请的APP SECRET 如 $secretKey="94dc99566550d87f8fa8ece112xxxxx"
private final String secretKey = "544ca4657ba8002e3dea3ac2f5fdd241";
// text 的内容为"欢迎使用百度语音合成"的urlencode,utf-8 编码
// 可以百度搜索"urlencode"
private final String text = "欢迎使用百度语音";
// 发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
private final int per = 0;
// 语速,取值0-9,默认为5中语速
private final int spd = 5;
// 音调,取值0-9,默认为5中语调
private final int pit = 5;
// 音量,取值0-9,默认为5中音量
private final int vol = 5;
public final String url = "http://tsn.baidu.com/text2audio"; // 可以使用https
private String cuid = "1234567JAVA";
private void run() throws Exception {
TokenHolder holder = new TokenHolder(appKey, secretKey, TokenHolder.ASR_SCOPE);
holder.resfresh();
String token = holder.getToken();
String url2 = url + "?tex=" + ConnUtil.urlEncode(text);
url2 += "&per=" + per;
url2 += "&spd=" + spd;
url2 += "&pit=" + pit;
url2 += "&vol=" + vol;
url2 += "&cuid=" + cuid;
url2 += "&tok=" + token;
url2 += "&lan=zh&ctp=1";
// System.out.println(url2); // 反馈请带上此url,浏览器上可以测试
HttpURLConnection conn = (HttpURLConnection) new URL(url2).openConnection();
conn.setConnectTimeout(5000);
String contentType = conn.getContentType();
if (contentType.contains("mp3")) {
byte[] bytes = ConnUtil.getResponseBytes(conn);
File file = new File("result.mp3"); // 打开mp3文件即可播放
// System.out.println( file.getAbsolutePath());
FileOutputStream os = new FileOutputStream(file);
os.write(bytes);
os.close();
System.out.println("mp3 file write to " + file.getAbsolutePath());
} else {
System.err.println("ERROR: content-type= " + contentType);
String res = ConnUtil.getResponseString(conn);
System.err.println(res);
}
}
}
5、实现效果
以上是 利用百度语音识别技术实现文字转语音的应用(Java版附源码) 的全部内容, 来源链接: utcz.com/z/394018.html