java - How to convert audio inpustream from google translate to wav file? -
i'm taking project make text speech google api : https://code.google.com/p/java-google-translate-text-to-speech/. used code in part "playing translated text". project works well. want create wav file inputstream received google translate. tried codes:
try { sound = audio.getaudio(text_, language.french); audioinputstream ais; ais = new audioinputstream(sound,format,sound.available()); //ais = audiosystem.getaudioinputstream(sound); //audioinputstream lowresais = audiosystem.getaudioinputstream(format, ais); audioformat aisformat = ais.getformat(); system.out.println(aisformat.tostring()); audiosystem.write(ais, type.wave, newfilepath);} } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }
but wav file doesn't work, when double-click play it, have no voice. have tried convert audio received google translate wav file in java? please helps me resolve problem? thanks
try this:
public static void main(string[] args) throws ioexception { string text ="hello hi how you"; url url = new url("http://translate.google.com/translate_tts?" + "q=" + text.replace(" ", "%20") + "&tl=" + "en"); urlconnection urlconn = url.openconnection(); urlconn.addrequestproperty("user-agent", "mozilla/4.0 (compatible; msie 6.0; windows nt 5.0)"); inputstream audiosrc = urlconn.getinputstream(); datainputstream read = new datainputstream(audiosrc); outputstream outstream = new fileoutputstream(new file("src/savedsound.wav")); byte[] barr = new byte[audiosrc.available()]; int len; while ((len = read.read(barr)) > 0) { outstream.write(barr, 0, len); } outstream.close(); }
Comments
Post a Comment