使用:
1.引入依赖
以Maven为例,pom文件中引入语音合成依赖
<dependency>
<groupId>com.alibaba.nls</groupId>
<artifactId>nls-sdk-tts</artifactId>
<version>2.2.14</version>
</dependency>
<dependency>
<groupId>com.alibaba.nls</groupId>
<artifactId>nls-sdk-common</artifactId>
<version>2.2.14</version>
</dependency>
2.代码导入
直接复制代码即可,导入只需要把appkey,id,secret,url改为自己的参数即可(下方会有讲解参数来源)
package cn.com.victorysoft.vsai.system.voice;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.nls.client.AccessToken;
import com.alibaba.nls.client.protocol.NlsClient;
import com.alibaba.nls.client.protocol.OutputFormatEnum;
import com.alibaba.nls.client.protocol.SampleRateEnum;
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizer;
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerListener;
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 此示例演示了:
* 语音合成API调用。
* 动态获取token。获取Token具体操作,请参见:https://help.aliyun.com/document_detail/450514.html
* 流式合成TTS。
* 首包延迟计算。
*/
public class SpeechSynthesizerDemo {
private static final Logger logger = LoggerFactory.getLogger(SpeechSynthesizerDemo.class);
private static long startTime;
private String appKey;
NlsClient client;
public SpeechSynthesizerDemo(String appKey, String accessKeyId, String accessKeySecret) {
this.appKey = appKey;
//应用全局创建一个NlsClient实例,默认服务地址为阿里云线上服务地址。
//获取token,使用时注意在accessToken.getExpireTime()过期前再次获取。
AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
try {
accessToken.apply();
System.out.println("get token: " + accessToken.getToken() + ", expire time: " +