代码之家  ›  专栏  ›  技术社区  ›  Dan

Bluemix:文本对语音的响应未使用以下代码播放音频

  •  1
  • Dan  · 技术社区  · 9 年前

    你能帮我解决这个问题吗?为什么文本对语音的响应不使用以下代码播放音频?

    索引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>
    

    演示Servlet.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方法都在响应中成功运行,它向jsp返回一些二进制类型的数据,ajax响应。 但我仍然无法播放音频。你能帮我解决这个问题吗?

    1 回复  |  直到 9 年前
        1
  •  1
  •   German Attanasio    9 年前

    问题是您正在为 src 的属性 <audio> 标签

    您需要执行以下操作:

    $.fn.PlayBtnClicked = function() { 
      var text = $(this).data("text"); 
    
      var audio = document.getElementById("myAudio");
      audio.setAttribute('src','/synthesize?text=' + encodeURIComponent(text));
    });
    

    更多信息