3

我需要实现一个可以阅读葡萄牙语文本的应用程序(TTS)。不幸的是,在使用TextToSpeech.setLanguage(Locale loc)方法时,我在其中找不到葡萄牙语。Locale.返回除葡萄牙语外的语言数。

那么,有什么办法可以用葡萄牙语实现 TTS 吗?无论如何,我正在使用 Android 2.3.3。

更新

正如一位成员建议的那样,我使用了 API

这是代码

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();
        }

我得到了错误

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)
4

4 回答 4

2

java-google-translate-text-to-speech怎么样?它是一个面向谷歌翻译的 API,提供了 Text To Speak 等功能。它支持葡萄牙语,这样的代码片段应该可以完成工作:

Audio audio = Audio.getInstance();
InputStream sound  = audio.getAudio("portuguese sentence", Language.PORTUGUESE);
audio.play(sound);

缺点是您必须在线才能使用 API。

于 2013-08-22T18:24:33.617 回答
2

您如何尝试获取语言环境?如果您依赖于Locale类中定义的常量,那么似乎没有为葡萄牙语定义一个常量。你应该能够像这样构建它:(new Locale("pt")new Locale("pt", "pt")new Locale("pt", "br")

如:

tts.setLanguage(new Locale("pt"));

可以使用检查语言的支持级别isLanguageAvailable(Locale)

于 2013-08-22T18:25:01.443 回答
0

尝试这个:

Locale locale = new Locale("pt", "br");
tts.setLanguage(locale);

它对我有用!

于 2019-06-23T13:36:56.880 回答
0

也许您没有安装需要的语言。

尝试安装 Google TTS:https ://play.google.com/store/apps/details?id=com.google.android.tts&hl=en ,然后在您的设备配置中选择它。

于 2016-04-07T13:55:06.327 回答