The Kahimyang Project Logo
Primo's Code Blog
Howtos tips and tricks
The Kahimyang Project

How to use Google Translate's Text to Speech (TTS) services in your web page using Servlet

This is complete working servlet that demonstrates how to use Google Translate's Text to Speech services. Google Translate TTS is able to translate text to clear voice for many different languages.

As a demo, please click play to speak the next sentence.

Loading the player ...


Here is the general format of the web services URL.

http://translate.google.com/translate_tts?tl=en&q=Text-to-speak

tl is the text language and q is the text that is to be spoken. Please visit Google Translate if the language you want to use is supported and change line 37 in the servlet code that follows for your language.

The web service returns an MP3 audio stream if the request is successful.

The Servlet

There is no special trick in this servlet except that you have to have a User-agent and NO Referer when connecting to the web services to avoid 403 and 404 errors respectively.

We use the same standard URL/URLConnection classes.


package kauswagan;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class GoogleTTS extends HttpServlet {

    String GURL =
            "http://translate.google.com/translate_tts?q=";

    @Override
    protected void doGet(HttpServletRequest request,
        HttpServletResponse response)
            throws ServletException, IOException {

        speak(request, response);

    }


    public void speak(HttpServletRequest request,
            HttpServletResponse response) throws
            ServletException, IOException {

        final String text = request.getParameter("text");

        URL url = new URL(GURL
                + text.replace(" ", "%20") + "&tl=en");

        URLConnection urlConn = url.openConnection();
        urlConn.addRequestProperty("User-Agent", "Mozilla");

        InputStream audioSrc = urlConn.getInputStream();
        DataInputStream read = new DataInputStream(audioSrc);

        ServletOutputStream out = response.getOutputStream();

        int b = 0;
        int c;

        while ((c = read.read()) != -1) {
            out.write(c);
            b++;
        }

        read.close(); audioSrc.close();

        response.setContentType("audio/mpeg");
        response.setContentLength(b);

        out.flush();
        out.close();
    }

}

The Servlet Descriptors

   <servlet>
        <servlet-name>GoogleTTS</servlet-name>
        <servlet-class>kauswagan.GoogleTTS</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>GoogleTTS</servlet-name>
        <url-pattern>/GoogleTTS</url-pattern>
    </servlet-mapping>

Player embed code.

<p>
    <div style="width:30px;height:30px;" id="jplayer">
          Loading the player ...</div>

    <script type="text/javascript">
        jwplayer("jplayer").setup({
            autostart: false,
            controlbar: "none",
            file: "/kauswagan/GoogleTTS?text=#{evNewsBlogsBean.titlex}",
            flashplayer: "./jw-5.8/player.swf",
            menu: false,
            volume: 80,
            width: 30,
            height: 30,
            provider: "sound"
        });
    </script>
</p>

Line 9 shown above is the call to our servlet and the text parameter which is the text we want spoken. We are using JWPlayer to play the voice. Use your favorite player as desired.

That's it. Good luck.

RSS Logo



Comment icon   Comments (Newest first)

Recent posts in Java/JSF category
Blue dot icon Using Rhinoslider with image and youtube content in JSF pages
Blue dot icon A dynamic standard sitemap.xml with Google image extension implemented as a Java Servlet
Blue dot icon Creating a Facebook-like panel with slim scrollbars and infinite scrolling in PrimeFaces
Blue dot icon Generating XML RSS 2 feeds with JDOM 2 with a servlet
Blue dot icon Building a page with infinite scroll in PrimeFaces using Waypoints jQuery plugin
Blue dot icon Implementing a collapsible ui:repeat rows in JSF
Blue dot icon Validating an email address in PrimeFaces p:inputText field with p:ajax





Most popular articles
Blue dot icon Using Expect script to automate SSH logins and do routine tasks accross multiple hosts   (15385)
Blue dot icon How to setup Tomcat 7 as your primary webserver on Debian Squeeze    (14082)
Blue dot icon How to setup FLV streaming with crtmpserver C++ RTMP server   (9856)
Blue dot icon A Java class for sending multipart Email messages through your Gmail account    (6979)
Blue dot icon How to use Google Translate's Text to Speech (TTS) services in your web page using Servlet   (5676)
Blue dot icon Speed up Primefaces page load with p:remoteCommand partial update   (5511)
Blue dot icon Building a mobile website with JSF 2 core and JQuery Mobile 1.0   (5328)
Blue dot icon Google Map-Adding markers, info window, circle, small control, and events in Javascript    (5227)