申请
https://ai.baidu.com/
创建应用
记录appid、 app key 、Secret Key

引入依赖
sdk github:https://github.com/Baidu-AIP/java-sdk
maven
1 2 3 4 5
| <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.12.0</version> </dependency>
|
编写代码
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
| import com.baidu.aip.speech.AipSpeech; import com.baidu.aip.speech.TtsResponse; import com.baidu.aip.util.Util; import org.json.JSONObject; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class Sample { public static final String APP_ID = "你的 App ID"; public static final String API_KEY = "你的 Api Key"; public static final String SECRET_KEY = "你的 Secret Key"; public static void main(String[] args) { AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY); client.setConnectionTimeoutInMillis(2000); client.setSocketTimeoutInMillis(60000);
HashMap<String,Object> map=new HashMap<>(); map.put("per","4"); TtsResponse res = client.synthesis("你好百度", "zh", 1, map); byte[] data = res.getData(); JSONObject res1 = res.getResult(); if (data != null) { try { Util.writeBytesToFileSystem(data, "D://output.mp3"); } catch (IOException e) { e.printStackTrace(); } } if (res1 != null) { System.out.println(res1.toString(2)); } } }
|
生成的文件

简单吧,手动滑稽