package com.nuvole.util; import com.baidu.aip.speech.AipSpeech; import com.baidu.aip.speech.TtsResponse; import lombok.extern.slf4j.Slf4j; import org.json.JSONObject; import java.util.HashMap; /** * 百度智能云工具类 * * @Author: lc * @Date: 2020/4/27 17:02 */ @Slf4j public class BaiduUtil { //详细请搜索百度智能云查看 //设置APPID/AK/SK private static final String APP_ID = "19624255"; private static final String API_KEY = "NqUfTfQUi31Gu5L0fD8YXRWZ"; private static final String SECRET_KEY = "DrFxoeyOEz23x3NUVb6GMo2PsZTiXk1a"; /** * 初始化一个AipSpeech */ private static AipSpeech aipSpeech = new AipSpeech(APP_ID, API_KEY, SECRET_KEY); /** * 语音合成 - 通过文字转化为mp3 * * @Author: lc * @Date: 2020/4/27 17:03 */ public static byte[] speech(String text) { // 可选:设置网络连接参数 aipSpeech.setConnectionTimeoutInMillis(2000); aipSpeech.setSocketTimeoutInMillis(60000); // 调用接口 HashMap options = new HashMap<>(16); /*语速,取值0-15,默认为5中语速*/ options.put("spd", 5); /*音调,取值0-15,默认为5中语调*/ options.put("pit", 5); /*音量,取值0-15,默认为5中音量*/ options.put("vol", 10); // 发音人选择, 基础音库:0为度小美,1为度小宇,3为度逍遥,4为度丫丫, // 精品音库:5为度小娇,103为度米朵,106为度博文,110为度小童,111为度小萌,默认为度小美 options.put("per", 0); TtsResponse res = aipSpeech.synthesis(text, "zh", 1, options); byte[] data = res.getData(); JSONObject res1 = res.getResult(); if (data != null) { return data; } if (res1 != null) { log.info(res1.toString(2)); } return null; } }