Twilio Voice API会播放mp3网址,而不是播放mp3音乐

时间:2017-12-19 06:13:04

标签: twilio twilio-api

我正在使用Twilio Voice API,并且每当检测到应答机/人时,我的应用程序都会有一个回调URL。我试图播放的mp3通过Twiml Runtime Bin工作正常。但是,当应用程序使用Twiml响应时,twilio总是会背诵URL而不是播放音乐。

public void makeOutBoundCall()
{
    Twilio.init(TWILIO_ACCOUNT_SID, TWILIO_ACCOUNT_TOKEN);

    CallCreator callCreator = new CallCreator(TWILIO_ACCOUNT_SID, new PhoneNumber(TO_NUMBER),
            new PhoneNumber(TWILIO_NUMBER),
            URI.create(APP_URL + TwilioVoiceApplication.TWILIO_AMD_CALLBACL_URL))
                    .setMachineDetection("DetectMessageEnd")
                    .setMachineDetectionTimeout(20000);
    callCreator.create();
} 


@RequestMapping(method = RequestMethod.POST)
public String amdCallback(@RequestParam Map<String, String> queryMap) throws Exception
{
    // Default behavior hangup the call
    Hangup hangup = new Hangup();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().hangup(hangup)
            .build();
    if (queryMap.containsKey("AnsweredBy"))
    {
        if (queryMap.get("AnsweredBy")
                .equals("human"))
        {
            voiceResponse = new VoiceResponse.Builder()
                    .say(new Say.Builder("Hello Monkey").build())
                    .play(new Play.Builder("http://demo.twilio.com/hellomonkey/monkey.mp3")
                            .build())
                    .build();
        }
    }
    return voiceResponse.toString();
}

1 个答案:

答案 0 :(得分:0)

@RequestMapping(method = RequestMethod.POST, produces = "application/xml")
public ResponseEntity<String> amdCallback(@RequestParam Map<String, String> queryMap)
            throws Exception
    {
    // Default behavior hangup the call
    Hangup hangup = new Hangup();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().hangup(hangup)
            .build();
    if (queryMap.containsKey("AnsweredBy"))
    {
        if (queryMap.get("AnsweredBy")
                .equals("human"))
        {
            voiceResponse = new VoiceResponse.Builder()
                    .say(new Say.Builder("Hello Monkey").build())
                    .play(new Play.Builder("http://demo.twilio.com/hellomonkey/monkey.mp3")
                            .build())
                    .build();
        }
    }
    HttpHeaders headers = new HttpHeaders();
    //Add the content-type:text/html in response header
    headers.add(HttpHeaders.CONTENT_TYPE, "text/xml");

    ResponseEntity<String> response = ResponseEntity.ok()
            .headers(headers)
            .body(voiceResponse.toXml());
    return response;
}