Reputation: 23025
I need to implement an app (TTS) which can read text in Portuguese language . Unfortunately, when using TextToSpeech.setLanguage(Locale loc)
method, I couldn't find Portuguese language in it. Locale.
returned number of languages except Portuguese.
So, is there any way I can implement TTS in Portuguese? I am using Android 2.3.3 anyway.
UPDATE
As one member suggested, I used the API
This is the code
private void speak()
{
String text = textToSpeech.getText().toString();
Audio audio = Audio.getInstance();
InputStream sound;
try {
sound = audio.getAudio(text, Language.PORTUGUESE);
audio.play(sound);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I got the error
08-23 15:00:47.421: W/System.err(728): java.net.UnknownHostException: translate.google.com
08-23 15:00:47.421: W/System.err(728): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
08-23 15:00:47.431: W/System.err(728): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
08-23 15:00:47.431: W/System.err(728): at java.net.InetAddress.getAllByName(InetAddress.java:256)
08-23 15:00:47.431: W/System.err(728): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
08-23 15:00:47.431: W/System.err(728): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
08-23 15:00:47.431: W/System.err(728): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
08-23 15:00:47.431: W/System.err(728): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
08-23 15:00:47.441: W/System.err(728): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
08-23 15:00:47.441: W/System.err(728): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
08-23 15:00:47.441: W/System.err(728): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
08-23 15:00:47.441: W/System.err(728): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
08-23 15:00:47.441: W/System.err(728): at com.gtranslate.Audio.getAudio(Audio.java:34)
08-23 15:00:47.451: W/System.err(728): at com.example.tts.MainActivity.speak(MainActivity.java:70)
08-23 15:00:47.451: W/System.err(728): at com.example.tts.MainActivity.access$0(MainActivity.java:63)
08-23 15:00:47.461: W/System.err(728): at com.example.tts.MainActivity$SpeechClass.onClick(MainActivity.java:53)
08-23 15:00:47.461: W/System.err(728): at android.view.View.performClick(View.java:2485)
08-23 15:00:47.461: W/System.err(728): at android.view.View$PerformClick.run(View.java:9080)
08-23 15:00:47.461: W/System.err(728): at android.os.Handler.handleCallback(Handler.java:587)
08-23 15:00:47.461: W/System.err(728): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 15:00:47.461: W/System.err(728): at android.os.Looper.loop(Looper.java:123)
08-23 15:00:47.471: W/System.err(728): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-23 15:00:47.471: W/System.err(728): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 15:00:47.471: W/System.err(728): at java.lang.reflect.Method.invoke(Method.java:507)
08-23 15:00:47.471: W/System.err(728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-23 15:00:47.471: W/System.err(728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-23 15:00:47.471: W/System.err(728): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 3
Views: 3234
Reputation: 1
Try this:
Locale locale = new Locale("pt", "br");
tts.setLanguage(locale);
It worked for me!
Upvotes: 0
Reputation: 11
Maybe you don't have the language that you need installed.
Try to install Google TTS: https://play.google.com/store/apps/details?id=com.google.android.tts&hl=en, and select it on your device configuration.
Upvotes: 0
Reputation: 33341
How are you trying to get the locale? If you are relying on the constants defined in the Locale
class, there doesn't seem to be one defined for Portuguese. You should be able to construct it something like: new Locale("pt")
(or new Locale("pt", "pt")
or new Locale("pt", "br")
)
Such as:
tts.setLanguage(new Locale("pt"));
level of support for the language can be checked using isLanguageAvailable(Locale)
.
Upvotes: 2
Reputation: 11958
How about java-google-translate-text-to-speech? It is an API towards Google Translate, exposing Text To Speak functions among others. It supports Portuguese, and a code snippet like this should do the work:
Audio audio = Audio.getInstance();
InputStream sound = audio.getAudio("portuguese sentence", Language.PORTUGUESE);
audio.play(sound);
The drawback is that you have to be online for the API to work.
Upvotes: 2