xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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<String, Object> 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;
    }
}