这是我的java代码:
public static void main(String[] args) {
TextToSpeech textService = new TextToSpeech(IBM_WATSON_USERNAME, IBM_WATSON_PASSWORD);
//String voice = "en-US_AllisonVoice";
String text = "This is Just awesome And i am going to experience the effect";
//String format = "audio/mp3";
try {
InputStream in = textService.synthesize(text, Voice.EN_ALLISON, AudioFormat.OGG_VORBIS)
.execute();
System.out.println(in.available());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
当我在eclipse中执行代码时,我得到了:
Dec 12, 2017 3:05:08 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice&accept=audio/ogg;%20codecs%3Dvorbis http/1.1 (71-byte body)
Dec 12, 2017 3:05:09 PM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice&accept=audio/ogg;%20codecs%3Dvorbis (588ms, unknown-length body)
in.available()的输出为:0
为什么我没有收到任何音频?根据POST请求,我可以看到我的文本没有被POSTED。我错过了什么?
答案 0 :(得分:1)
来自available()
的{{1}}方法将始终返回0,因为它取决于InputStream
的实现。请参阅InputStream
Javadoc。
调用InputStream
时得到的InputStream,它是来自synthesize()
库的byteStream()。
您需要从okHttp
读取到文件或其他位置。
以下是可用于此的代码段:
InputStream
注意:上面的代码段没有任何inputStream = textToSpeech.synthesize(/* parameters*/).execute();
outputStream = new FileOutputStream(new File("audio.ogg"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
System.out.println("Done!");
,也没有关闭流。我会把它留给你=)