使用Twilio Java Sdk v7.x将Twilio Recording作为InputStream下载

时间:2017-05-18 04:18:45

标签: java twilio twilio-api

我正在努力将当前的Twilio Java SDK客户端从版本6.x升级到版本7.x. 我遇到的一个问题是检索录制的InputStream。 下面是我所拥有的代码的6.x版本示例,但是找不到在版本7.x中为记录检索InputStream的方法。 (目前7.0.0-rc10) 你能否指导我做错了什么?

代码段版本=" 6.x"

public InputStream retrieveRecording(String recordingSid) {
    Recording recordingToRetrieve = new Recording(twilioRestClient, recordingSid);
    recordingToRetrieve.setRequestAccountSid("xxxxxxxxx");
    return recordingToRetrieve.getMedia(".mp3");
}

代码段版本=" 7.x"

public InputStream retrieveRecording(String recordingSid) {
    Recording recordingToRetrieve = Recording.fetch("xxxxxxxxx", recordingSid).execute();
    //How do I get the mp3 media as an Input stream ?
}

1 个答案:

答案 0 :(得分:1)

不幸的是,目前在Twilio SDK V7 7.0.0-rc-10中没有简单的方法来检索录制的InputStream,理想情况下应该将其内置到SDK中,但同时这里我是如何解决的检索下载的问题

 private InputStream retrieveRecording(String accountSid, String authToken, String recordingSid) {
    Twilio.init(accountSid, authToken);
    Recording recordingToRetrieve = Recording.fetch(accountSid, recordingSid).execute();
    String uri = recordingToRetrieve.getUri();
    String mp3Uri = uri.replace(".json", ".mp3");
    Request request = new Request(
            HttpMethod.GET,
            TwilioRestClient.Domains.API,
            mp3Uri,
            accountSid);
    Response mp3response = Twilio.getRestClient().request(request);
    if (mp3response == null) {
        throw new ApiConnectionException("Recording media fetch failed: Unable to connect to server");
    } else if (!TwilioRestClient.SUCCESS.apply(mp3response.getStatusCode())) {
        RestException restException = RestException.fromJson(mp3response.getStream(), Twilio.getRestClient().getObjectMapper());
        if (restException == null) {
            throw new ApiException("Server Error, no content");
        }

        throw new ApiException(
                restException.getMessage(),
                restException.getCode(),
                restException.getMoreInfo(),
                restException.getStatus(),
                null);
    }
    return mp3response.getStream();
}