Bluemix:文本到语音的响应不是使用下面的代码播放音频

时间:2015-08-23 06:11:30

标签: ibm-cloud ibm-watson

你能帮忙解决这个问题,为什么Text-to-speech的响应不是使用下面的代码播放音频?

的index.jsp

$.fn.PlayBtnClicked = function() { 
    var txt=$(this).data("text"); 
    $.ajax({
      type: "GET",
      url: 'demo',async: false,
      data: { text: txt} ,
        success: function(response) { 
        $(".result").show();
            var audio = document.getElementById("myAudio"); 
            audio.src = response;
            audio.type = "type/ogg"; 
            audio.play();
        }
     });
 }; 

<audio id="myAudio" autoplay preload="auto" autobuffer controls class="audio"></audio>

DemoServlet.java

protected void doGet(final HttpServletRequest req,
            final HttpServletResponse resp) throws ServletException,
            IOException {
        String serviceName = "text_to_speech";

        // If running locally complete the variables below with the information
        // in VCAP_SERVICES
        String baseURL = "https://stream.watsonplatform.net/text-to-speech/api";
        String username = "USERNAME";
        String password = "PASSWORD";

        if (req.getParameter("text") == 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  text=req.getParameter("text");
                text=URLEncoder.encode(text, "UTF-8");
                String voice="&voice=en-US_AllisonVoice";
                String queryStr=text+voice;
                String url = baseURL + "/v1/synthesize";
                if (queryStr != null) {
                    url += "?text=" + 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);
            }
        }
    }

这里两个java moethod成功运行以响应它将一些二进制类型的数据返回给jsp,ajax响应。     但我仍然无法播放音频。你能帮我解决一下这个问题吗?

1 个答案:

答案 0 :(得分:1)

问题是您要对src标记的<audio>属性设置html回复。

您需要执行以下操作:

$.fn.PlayBtnClicked = function() { 
  var text = $(this).data("text"); 

  var audio = document.getElementById("myAudio");
  audio.setAttribute('src','/synthesize?text=' + encodeURIComponent(text));
});

更多信息