阅读背景:

Java阿里云语音Demo

来源:互联网 

  主要依赖得包有2个aliyun-java-sdk-core.jar 和 aliyun-java-sdk-dyvmsapi.jar,直接上代码。

 1 package message.demo;
 2 
 3 import com.aliyuncs.DefaultAcsClient;
 4 import com.aliyuncs.IAcsClient;
 5 import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsRequest;
 6 import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsResponse;
 7 import com.aliyuncs.exceptions.ClientException;
 8 import com.aliyuncs.profile.DefaultProfile;
 9 import com.aliyuncs.profile.IClientProfile;
10 
11 public class VoiceDemo {
12 
13     // 云通信产品-语音API服务产品名称(产品名固定,无需修改)
14     static final String product = "Dyvmsapi";
15     // 产品域名(接口地址固定,无需修改)
16     static final String domain = "dyvmsapi.aliyuncs.com";
17     // AK信息
18     static final String accessKeyId = "LTAI1pjHEjVWmzhs";
19     
20     static final String accessKeySecret = "Kf9ukSKVk8BWWa1cwGdb7ylZPLhHLG";
21     
22     public static SingleCallByTtsResponse sendCVoice() throws ClientException{
23         //设置访问超时时间
24         System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
25         System.setProperty("sun.net.client.defaultReadTimeout", "10000");
26         
27         //初始化acsClient 暂时不支持多region
28         IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
29         DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
30         IAcsClient acsClient = new DefaultAcsClient(profile);
31 
32         SingleCallByTtsRequest request = new SingleCallByTtsRequest();
33         //必填-被叫显号,可在语音控制台中找到所购买的显号
34         request.setCalledShowNumber("055166160359");
35         //必填-被叫号码
36         request.setCalledNumber("15000000000");
37         //必填-Tts模板ID
38         request.setTtsCode("TTS_127163655");
39          //可选-当模板中存在变量时需要设置此值
40         //request.setTtsParam("{\"code\":\"123456\"}");
41         //可选-音量 取值范围 0--200
42         request.setVolume(200);
43         //可选-播放次数
44         request.setPlayTimes(5);
45         //可选-外部扩展字段,此ID将在回执消息中带回给调用方
46         request.setOutId("111");
47         
48         SingleCallByTtsResponse singleCallByTtsResponse = acsClient.getAcsResponse(request);
49         
50         if(singleCallByTtsResponse.getCode() != null && singleCallByTtsResponse.getCode().equals("OK")) {
51             //请求成功
52        System.out.println("语音文本外呼---------------");
53        System.out.println("RequestId=" + singleCallByTtsResponse.getRequestId());
54        System.out.println("Code=" + singleCallByTtsResponse.getCode());
55        System.out.println("Message=" + singleCallByTtsResponse.getMessage());
56        System.out.println("CallId=" + singleCallByTtsResponse.getCallId());
57    }
58         
59         return singleCallByTtsResponse; 
60     }
61     
62     public static void main(String[] args) throws ClientException {
63         
64         sendCVoice();
65     }
66 
67 }

 


分享到: