2

I am a beginner in java and I followed a video tutorial about text to speech in java in this link: https://www.youtube.com/watch?v=ameD1ocbn9s

In here,I faced a problem with the eclipse mars when compiling this program.It says as follows:

Here is my code:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.text.MessageFormat;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import us.monoid.web.BinaryResource;
import us.monoid.web.Resty;

//here,we loaded the javaLayer and resty lib files to this project

public class TextToSpeech
{   
    //base url from google translator to get the sound,
    //tts-text to speech ,q-translating text,tl-translating language
    private static final String Base_URL="http://translate.google.com/translate_tts?ie=UTF-8&q={0}&tl={1}&prev=input";


    public static void main(String[] args) 
    {
        speech("hello kalanka");
    }
    public static void speech(String text)
    {

        try 
        {
            File file=new File("temp.mp3");//temp file to save the sound from google

            String sentence= URLEncoder.encode(text,"UTF-8");//encoding the text we send as utf-8

            String urlString=MessageFormat.format(Base_URL,sentence,"en");// filling the baseUrl with arguments 
            //"en" means the English language

            BinaryResource res=new Resty().bytes(new URI(urlString));//creating a rest object for the binary resources to get the mp3 file 
            //which will receive from google

            res.save(file);//saving the binary res file in file that we created

            FileInputStream in=new FileInputStream(file);

            Player pp=new Player(in);//creating the Player object

            pp.play();//playing the file 
            pp.close();//closing the play
            file.delete();//deleting the file

        } 
        catch (UnsupportedEncodingException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        } 
        catch (URISyntaxException e)
        {
            e.printStackTrace();
        } catch (JavaLayerException e)
        {
            e.printStackTrace();
        }


    }

}

Here is the error in full:

    java.io.IOException: Error while reading from GET: [503] Service Unavailable
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta name="viewport" content="initial-scale=1"><title>http://translate.google.com/translate_tts?ie=UTF-8&amp;q=hello+kalanka&amp;tl=en&amp;prev=input</title></head>
<body style="font-family: arial, sans-serif; background-color: #fff; color: #000; padding:20px; font-size:18px;" onload="e=document.getElementById('captcha');if(e){e.focus();}">
<div style="max-width:400px;">
 <hr noshade size="1" style="color:#ccc; background-color:#ccc;"><br>

  To continue, please type the characters below:<br><br>
  <img src="/sorry/image?id=15657022896595218819&amp;q=CGMSBGf3MFUY-PWSuQUiGQDxp4NLgYkSgFOxxcQGJWJm5jZQLxCJi6c&amp;hl=en&amp;continue=http://translate.google.com/translate_tts%3Fie%3DUTF-8%26q%3Dhello%2Bkalanka%26tl%3Den%26prev%3Dinput" border="1" alt="Please enable images"><br><br><form action="CaptchaRedirect" method="get"><input type='hidden' name='q' value='CGMSBGf3MFUY-PWSuQUiGQDxp4NLgYkSgFOxxcQGJWJm5jZQLxCJi6c'><input type="hidden" name="continue" value="http://translate.google.com/translate_tts?ie=UTF-8&amp;q=hello+kalanka&amp;tl=en&amp;prev=input"><input type="hidden" name="id" value="15657022896595218819"><input type="text" name="captcha" value="" id="captcha" size="12" style="font-size:16px; padding:3px 0 3px 5px; margin-left:0px;"><input type="submit" name="submit" value="Submit" style="font-size:18px; padding:4px 0;"><br><br><br></form>
  <hr noshade size="1" style="color:#ccc; background-color:#ccc;">

   <div style="font-size:13px;">
    <b>About this page</b><br><br>Our systems have detected unusual traffic from your computer network.  This page checks to see if it&#39;s really you sending the requests, and not a robot.  <a href="#" onclick="document.getElementById('infoDiv').style.display='block';">Why did this happen?</a><br><br>
    <div id="infoDiv" style="display:none; background-color:#eee; padding:10px; margin:0 0 15px 0; line-height:1.4em;">
     This page appears when Google automatically detects requests coming from your computer network which appear to be in violation of the <a href="//www.google.com/policies/terms/">Terms of Service</a>. The block will expire shortly after those requests stop.  In the meantime, solving the above CAPTCHA will let you continue to use our services.<br><br>This traffic may have been sent by malicious software, a browser plug-in, or a script that sends automated requests.  If you share your network connection, ask your administrator for help &mdash; a different computer using the same IP address may be responsible.  <a href="//support.google.com/websearch/answer/86640">Learn more</a><br><br>Sometimes you may be asked to solve the CAPTCHA if you are using advanced terms that robots are known to use, or sending requests very quickly.
    </div>




 IP address: 103.247.48.85<br>Time: 2016-04-30T14:02:34Z<br>URL: http://translate.google.com/translate_tts?ie=UTF-8&amp;q=hello+kalanka&amp;tl=en&amp;prev=input<br>
 </div>
</div>
</body>
</html>

    at us.monoid.web.AbstractResource.fill(AbstractResource.java:51)
    at us.monoid.web.Resty.fillResourceFromURL(Resty.java:432)
    at us.monoid.web.Resty.doGET(Resty.java:388)
    at us.monoid.web.Resty.bytes(Resty.java:355)
    at main.TextToSpeech.speech(TextToSpeech.java:42)
    at main.TextToSpeech.main(TextToSpeech.java:28)
Caused by: java.io.IOException: Server returned HTTP response code: 503 for URL: http://ipv4.google.com/sorry/IndexRedirect?continue=http://translate.google.com/translate_tts%3Fie%3DUTF-8%26q%3Dhello%2Bkalanka%26tl%3Den%26prev%3Dinput&q=CGMSBGf3MFUY-PWSuQUiGQDxp4NLgYkSgFOxxcQGJWJm5jZQLxCJi6c
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at us.monoid.web.AbstractResource.fill(AbstractResource.java:30)
    ... 5 more

Please help me in this code....

Kalanka
  • 1,019
  • 2
  • 18
  • 31
  • 1
    Give the _full_ exception stack trace. That includes the first line that tells you what exception is actually being thrown. – bcsb1001 Apr 30 '16 at 11:35
  • 1
    @bcsb1001 I have edited the question with the full error message.but I think there is something with the eclipse in connecting to google translator.I dont't know what to do.Same error message is coming with netbeans too.please help... – Kalanka Apr 30 '16 at 16:35

1 Answers1

0

You are getting a Captcha blocking your request. This is because you are accessing the site programatically thought the site is designed to be used manually.

Check out discussion and potential solutions here: Google Translate TTS API blocked

Or consider looking for alternative APIs:

Community
  • 1
  • 1
Scott Newson
  • 2,745
  • 2
  • 24
  • 26