Watson 文本转语音不产生音频输出
Watson Text to speech not producing Audio output
这是我的 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。我缺少什么?
InputStream
中的 available()
方法将始终 returns 0,因为它取决于 InputStream
的实现。请参阅 InputStream
Javadoc。
调用 synthesize()
时获得的 InputStream 是 okHttp
库中的 byteStream()。
您需要从 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!");
注意: 上面的代码片段没有任何 try{}catch
并且它不会关闭流。我会把它留给你 =)
这是我的 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。我缺少什么?
InputStream
中的 available()
方法将始终 returns 0,因为它取决于 InputStream
的实现。请参阅 InputStream
Javadoc。
调用 synthesize()
时获得的 InputStream 是 okHttp
库中的 byteStream()。
您需要从 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!");
注意: 上面的代码片段没有任何 try{}catch
并且它不会关闭流。我会把它留给你 =)