android - 以编程方式为 TTS 设置语言?

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

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

最佳答案

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

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

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

关于android - 以编程方式为 TTS 设置语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6656536/

相关文章:

java - 如何在 Firebase Android 中获取带有位置的帖子 key

android - 解析服务器错误的 json 响应错误代码 100

database - 这是支持 x 种语言的多语言系统文本的良好设计吗?

Laravel 4 多语言网站

java - 如何设置多语言文本到语音

android - 如何在android中找到tts.SythensizetoFile已经完成

android - 文字转语音(TTS)-Android-葡萄牙语

android - 设备休眠时服务进入待机状态 - Android

android - 为 Android 构建问题 Mailcore2 库

javascript - 如何开发网站多语言切换?