How to get a recorded wav file using Twilio Java API

牧云@^-^@ 提交于 2021-02-11 10:06:33

问题


Could somebody point me, using Twilio Java API, NOT! REST requests, how can I get the recorded file (.wav) of a concrete call.

I have read all the related articles to recording (https://support.twilio.com/hc/en-us/sections/205104748-Recording), but none of them shows how to do that with Java API.

I use this code, as an starting point, assuming the CALL_SID is known:

import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.api.v2010.account.Recording;
import com.twilio.rest.api.v2010.account.RecordingReader;

public class DeleteRecordings1 {

    private static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX";
    private static final String AUTH_TOKEN = "999aa999aaa999aaaa999";

    private static final String CALL_SID = "CA83837718818gdgdg";

    public static void main(String[] args) {
        try {       
            Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

            RecordingReader recordingReader = Recording.reader();
                recordingReader.setCallSid(CALL_SID);

            ResourceSet<Recording> recordings = recordingReader.read();
            String recordingSid;
            for (Recording recording: recordings) { 
                recordingSid = recording.getSid();              

                //HERE! I want to restore the .wav file associated with that RECORD_SID ?¿

            }                       
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

回答1:


Once you know the recordingSid for example RE557ce644e5ab84fa21cc21112e22c485

you can get a .wav file at https://api.twilio.com/2010-04-01/Accounts/ACXXXXX.../Recordings/RE557ce644e5ab84fa21cc21112e22c485.wav

You can get a .mp3 file at https://api.twilio.com/2010-04-01/Accounts/ACXXXXX.../Recordings/RE557ce644e5ab84fa21cc21112e22c485.mp3

where ACXXXXX... is your Twilio account SID (ACCOUNT_SID)




回答2:


I put the final code, in case it can help somebody:

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.api.v2010.account.Recording;
import com.twilio.rest.api.v2010.account.RecordingReader;

public class GetCallRecordings {

    private static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX";
    private static final String AUTH_TOKEN = "999aa999aaa999aaaa999";

    private static final String CALL_SID = "CA83837718818gdgdg";

    private static final String TWILIO_RES_URL = "https://api.twilio.com/2010-04-01/Accounts";
    private static final String REC_EXT = ".mp3";

    private static final String RUTA_RECS = "C:/recursos/grabaciones/";

    public static void main(String[] args) {
        try {
            Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

            RecordingReader recordingReader = Recording.reader();
            recordingReader.setCallSid(CALL_SID);

            ResourceSet<Recording> recordings = recordingReader.read();

            String recordingSid;
            String urlGrabacion;
            String locGrabacion;

            InputStream in;

            for (Recording recording : recordings) {
                recordingSid = recording.getSid();
                urlGrabacion = TWILIO_RES_URL + "/" + ACCOUNT_SID + "/Recordings/" + recordingSid + REC_EXT;
                locGrabacion = RUTA_RECS + CALL_SID + "_" + recordingSid + REC_EXT;

                System.out.println("Recuperando grabacion " + recordingSid);
                System.out.println("Ubicacion remota " + urlGrabacion);

                if (!new File(RUTA_RECS).exists()) {
                    new File(RUTA_RECS).mkdirs();
                }

                in = new URL(urlGrabacion).openStream();
                Files.copy(in, Paths.get(locGrabacion), StandardCopyOption.REPLACE_EXISTING);

                System.out.println("Ubicacion local " + locGrabacion);

                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


来源:https://stackoverflow.com/questions/59842242/how-to-get-a-recorded-wav-file-using-twilio-java-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!