简体   繁体   English

如何从Java发送Bluemix WAV流响应

[英]How to send bluemix wav stream response from java

I'd like to invoke IBM Bluemix service (say Text to Speech) from my Java code. 我想从我的Java代码中调用IBM Bluemix服务(例如,文本到语音)。 I've managed to get service credentials and URL but how can I invoke it after? 我已经设法获得服务凭证和URL,但是之后如何调用它呢?

I've seen some example where people have used similar to below code but wondering how it works for a Text to Speech where it outputs a wav stream. 我已经看到了一些示例,其中人们使用了类似于以下代码的示例,但想知道它如何在输出wav流的文本到语音中工作。

  String profileString = ex.execute(profileRequest)
         .handleResponse(new ResponseHandler<String>() {
    @Override
     public String handleResponse(HttpResponse r)
      throws ClientProtocolException, IOException {
      }
    }

Can any one suggest on priority please? 有人可以优先提出建议吗?

The link below has a Java code example of how to use the Watson text-to-speech service. 下面的链接提供了有关如何使用Watson文本语音转换服务的Java代码示例。

https://github.com/watson-developer-cloud/text-to-speech-java https://github.com/watson-developer-cloud/text-to-speech-java

You should be looking for something like this from the DemoServlet.java class: 您应该从DemoServlet.java类中寻找类似的东西:

@Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        if (req.getParameter("text") == null || req.getParameter("voice") == null) {
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } else {
            boolean download = false;
            if (req.getParameter("download") != null && req.getParameter("download").equalsIgnoreCase("true")) {
                download = true;
            }

            req.setCharacterEncoding("UTF-8");
            try {
                String queryStr = req.getQueryString();
                String url = baseURL + "/v1/synthesize";
                if (queryStr != null) {
                    url += "?" + queryStr;
                }
                URI uri = new URI(url).normalize();

                Request newReq = Request.Get(uri);
                newReq.addHeader("Accept", "audio/ogg; codecs=opus");

                Executor executor = Executor.newInstance().auth(username, password);
                Response response = executor.execute(newReq);
                if (download)
                {
                    resp.setHeader("content-disposition", "attachment; filename=transcript.ogg");
                }
                ServletOutputStream servletOutputStream = resp.getOutputStream();
                response.returnResponse().getEntity()
                .writeTo(servletOutputStream);
                servletOutputStream.flush();
                servletOutputStream.close();
            } catch (Exception e) {
                // Log something and return an error message
                logger.log(Level.SEVERE, "got error: " + e.getMessage(), e);
                resp.setStatus(HttpStatus.SC_BAD_GATEWAY);
            }
        }
    }

Finally, the link below has instructions on how to create a Java war file and deploy to Bluemix: 最后,下面的链接提供了有关如何创建Java war文件并将其部署到Bluemix的说明:

https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/getting_started/gs-full-java.shtml https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/getting_started/gs-full-java.shtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM