以编程方式设置TTS语言?

9
我写了一个小型的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();
} 

}

1个回答

5

谢谢Femi,我也试过了,但好像还是不行:S - Ignacio
4
我终于解决了问题!我不得不手动取消“文本转语音设置”下的“始终使用我的设置”复选框。 - Ignacio
@Ignacio 在三星Galaxy S3手机中,我在哪里可以找到这些设置? - Shajeel Afzal
@Ignacio...谢谢!!!我都快疯了!!!现在我想知道是否可以通过编程实现,那将非常好! - Phantômaxx
1
@Femi,我只是想知道,如果用户手动安装了一些语言包(不受“TTS”支持),那么“TTS”是否能为这些语言工作? - maddy d
在链接中提到Locale.ITALIAN有一些“相当有趣的结果”,我无法在T867设备上在法语之后运行它,但在N950F三星设备上运行正常。 - Bay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接