【问题标题】:Setting language for TTS programmatically?以编程方式为 TTS 设置语言?
【发布时间】:2011-10-03 03:32:21
【问题描述】:

我编写了一个小的 Android Demo 来使用不同语言的 TTS。我有一个带有两个按钮的布局,西班牙语和英语。按下按钮会触发所选语言的话语。

但是,我无法更改语言 (setLanguage (Locale locale))。我可以手动完成,使用手机设置并将 TTS 语言更改为美国、英国、意大利语、德语等,但我的代码似乎不起作用。你能告诉我问题出在哪里吗?

谢谢!!

package com.ignacio.SpeakAPP;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import java.util.Locale;

public class SpeakAPPActivity extends Activity implements OnInitListener {
private static final String TAG = "TextToSpeechDemo";
private TextToSpeech mTts;
public boolean Passer = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

/** Handle the action of the English Button **/
public boolean talknowEN(View v)
{

    mTts = new TextToSpeech (this, this);
    return Passer = false;
}

/** Handle the action of the Spanish Button **/
public boolean talknowES(View v)
{
    mTts = new TextToSpeech (this, this);   
    return Passer = true;
}

/** TTS **/
public void onInit (int status){

    if (status ==TextToSpeech.SUCCESS){

        if(Passer==false){
            //If English Button was activated
            //Initialize speech to text, set language to english and send utterance
            mTts.setLanguage(Locale.US);
            mTts.speak("How may I help you?", TextToSpeech.QUEUE_FLUSH, null);  
        }else{
            //If Spanish Button was activated
            //Initialize speech to text, check if spanish is available, set locale to spanish and send utterance

            Locale loc = new Locale ("es", "ES");
            mTts.setLanguage(loc);
            if (result2==TextToSpeech.LANG_MISSING_DATA||result2==TextToSpeech.LANG_NOT_SUPPORTED){
                Log.e(TAG, "Language is not available");
            }else {
                mTts.speak("Como puedo ayudarte?", TextToSpeech.QUEUE_FLUSH, null);
            }

        }

    }else {
        Log.e(TAG, "Could not initialize TextToSpeech");
    }

}


@Override
protected void onDestroy(){
    super.onDestroy();
    mTts.shutdown();
} 

}

【问题讨论】:

    标签: android multilingual text-to-speech speech-synthesis


    【解决方案1】:

    来自https://web.archive.org/web/20120505124037/http://developer.android.com/resources/articles/tts.html,您可能想试试这个:

    Locale loc = new Locale ("spa", "ESP");
    

    看起来很奇怪,但这就是他们所引用的(不是人们期望的es)。

    【讨论】:

    • 谢谢 Femi,我也试过了,但它似乎也不起作用:S
    • 我终于解决了这个问题!我不得不手动取消选中文本到语音设置下的“始终使用我的设置”复选框。
    • @Ignacio 我可以在三星 Galaxy S3 中找到这些设置
    • @Ignacio ...谢谢!我快疯了!!!!现在我想知道我是否可以通过编程方式实现它,那真是太好了!
    • @Femi,只想知道如果用户手动安装一些语言包(TTS 不支持),TTS 是否适用于这些语言?。
    猜你喜欢
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    相关资源
    最近更新 更多