如何在 java-sdk API 中调用 TextToSpeech 服务的合成方法?

How do I call the synthesize method of the TextToSpeech service within the java-sdk API?

可在此处找到 API 文档:https://github.com/watson-developer-cloud/java-sdk

当我尝试使用该服务时,它正确地进行了身份验证,然后在合成方法上失败了。

 TextToSpeech tts_service = new TextToSpeech();
 tts_service.setUsernameAndPassword("<username>", "<password>");

 tts_service.synthesize("The cat sat on the mat", Voice.EN_LISA, "audio/ogg; codecs=opus");

错误的堆栈跟踪如下所示。我也尝试过没有语音和格式参数的合成方法(因为它有默认值)但是当我这样做时服务失败并出现同样的错误。

Nov 25, 2015 4:58:55 PM com.ibm.watson.developer_cloud.service.WatsonService execute SEVERE: https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?text=The%20cat%20sat%20on%20the%20mat&voice=en-US_LisaVoice&Accept=audio%2Fogg%3B%20codecs%3Dopus, status: 400, error: The argument(s) [u'Accept'] are not allowed. Nov 25, 2015 4:58:55 PM com.vaadin.server.DefaultErrorHandler doDefault SEVERE: com.ibm.watson.developer_cloud.service.BadRequestException: The argument(s) [u'Accept'] are not allowed. at com.ibm.watson.developer_cloud.service.WatsonService.execute(WatsonService.java:128) at com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech.synthesize(TextToSpeech.java:119)

我希望得到一些帮助,我想使用 java API 而不是 REST 调用。

谢谢。

Java-SDK 正在发送 Accept 而不是 accept(棘手的错误)。

我们下一个版本之前的解决方法是扩展 TextToSpeech class 并覆盖:

InputStream synthesize(final String text, final Voice voice, final String outputFormat)

示例:

public class TextToSpeechHotFix extends TextToSpeech {

  @Override
  public InputStream synthesize(final String text, final Voice voice, final String outputFormat) {
    final RequestBuilder request = RequestBuilder.get(PATH_SYNTHESIZE);
    request.withQuery(TEXT, text);
    request.withQuery(VOICE, voice.getName());

    if (outputFormat != null && !outputFormat.startsWith("audio/"))
      throw new IllegalArgumentException(
          "format needs to be an audio mime type, for example: audio/wav or audio/ogg; codecs=opus");

    request.withQuery(ACCEPT, outputFormat != null ? outputFormat : HttpMediaType.AUDIO_WAV);

    final Response response = execute(request.build());
    return ResponseUtil.getInputStream(response);
  }
}