Reputation: 303
I need to send a request to an API through the service (t = textToSpeechService.callAPI(tmp);) so that it returns to me an audio wav file. What I am having a problem with is that, I don't know what classes should I use instead of Clip (public Clip callAPI(Source src)) in order to capture that file and forward it in the front end i.e. the front end that calls this service? By this I mean that I don't want the file to be played in the back-end as in the (callAPI function) but in a way to capture the file and then play, pause, or stop it in the front end. How can I represent an audio file in Java, I am very confused? Isn't there something like Audio class as in Javascript?
public Clip callAPI(Source src){
URL url;
Clip result = null;
AudioInputStream sound = null;
{
try {
url = new URL(" http://api.voicerss.org/?key=" + keyAPI + "&hl=" + src.getLang() + "&src=" + src.getSrc());
sound = AudioSystem .getAudioInputStream(url); //here i have the audio
Object sound2 = AudioSystem.getAudioInputStream(url);
AudioFormat at = sound.getFormat();
result = AudioSystem.getClip();
result.open(sound);
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (LineUnavailableException e) {
e.printStackTrace();
}
}
return result;
}
Clip t = null;
AudioInputStream ais = null;
private TextToSpeechService textToSpeechService;
public Clip theFunction(@RequestParam String src, HttpServletRequest request){
//if(src == request.getSession().getAttribute("input")){
Source tmp = new Source();
tmp.setSrc(src);
t = textToSpeechService.callAPI(tmp);
t.start();
return t;
}
Upvotes: 2
Views: 442
Reputation: 743
AudioInputStream extends InputStream. In theory, reading the audio as stream of bytes until EOF
(-1) and writing those bytes into the output stream of the HTTP (servlet) response will transfer the audio to the browser; most likely a specific "content-type" header will have to be set first on the response to denote the type of audio/audio format and perhaps some additional audio characteristics from AudioFormat.
Upvotes: 1