本章节为您介绍了阿里云语音服务语音通知的功能介绍和实现方法,您可以通过调用SingleCallByVoice实现发起语音文件类型的语音通知、调用SingleCallByTts实现发起文本转语音类型的语音通知。

功能介绍

语音通知可以很好的解决通知到达率延迟、关注度不高、阅读体验差等问题,并且您可以获通过电话获得更全面的信息通知。您只需要提前设置通知模版内容,调用API就可以给用户推送语音通知,并可以设置播放音量、播放次数等。

实现方法

在实现调用API实现语音通知功能前,您需要完成以下操作。

  • 购买语音号码并审核通过语音模板或语音文件,具体操作请参见快速入门
  • 获取AccessKey信息,详情请参见获取AccessKey
  • 完成本地SDK环境搭建,详情请参见Java SDK

本章节调用接口参数详情,请参见SingleCallByVoiceSingleCallByTts

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class yuyinTest {
    private static  String SingleCallByVoice (IAcsClient client,String Domain,String Version ,MethodType Method) throws ClientException{
        CommonRequest request = new CommonRequest();
        request.setDomain(Domain);
        request.setVersion(Version);
        request.setMethod(Method);
        request.setAction("SingleCallByVoice");
        request.putQueryParameter("CalledShowNumber", "18850xxxx");
        request.putQueryParameter("CalledNumber", "15750xxxx");
        request.putQueryParameter("VoiceCode", "e271f3f2-e155-4366-a9f4-0fe55765b3ec.wav");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("发起语音文件通知返回");
            System.out.println(JSON.toJSONString(response.getData()));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static String SingleCallByTts (IAcsClient client,String Domain,String Version ,MethodType Method) throws ClientException{
        CommonRequest request = new CommonRequest();
        request.setDomain(Domain);
        request.setVersion(Version);
        request.setMethod(Method);
        request.setAction("SingleCallByTts");
        request.putQueryParameter("CalledShowNumber", "18850xxxx");
        request.putQueryParameter("CalledNumber", "15750xxxx");
        request.putQueryParameter("TtsCode", "TTS_130555018");
        request.putQueryParameter("TtsParam", "{\"name\":\"测试\",\"time\":\"2019年\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("文本转语音类型返回");
            System.out.println(JSON.toJSONString(response.getData()));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String []args) throws Exception {
        //创建profile对象,并传入AccessKey信息,RegionId默认cn-hangzhou
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIxxxx", "Q3STpxxxx");
        IAcsClient client = new DefaultAcsClient(profile);
        //设置请求接口域名、版本号、请求方式
        final String Domain = "dyvmsapi.aliyuncs.com";
        final String Version = "2017-05-25";
        final MethodType Method = MethodType.POST;
        String SingleCallByVoice = SingleCallByVoice(client,Domain,Version,Method);
        String SingleCallByTts = SingleCallByTts(client,Domain,Version,Method);
    }
}

正常返回结果如下所示:

发起语音文件通知返回
{
  "Message": "OK",
  "RequestId": "4ABF7FB6-449C-4624-8CC6-9AAE368453AA",
  "Code": "OK",
  "CallId": "118720962490^10551812xx"
}
文本转语音类型返回
{
  "Message": "OK",
  "RequestId": "762104B0-D58A-4D04-8486-7C5171D0D756",
  "Code": "OK",
  "CallId": "118720797955^10551795xx"
}

调用SDK返回错误码,详情请参见错误码列表