设置语言TTS编程? [英] Setting language for TTS programmatically?

查看:180
本文介绍了设置语言TTS编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个小机器人演示使用TTS不同的语言。我有两个按钮,西班牙语和英语的布局。 pressing按钮触发话语中所选的语言。

I have written a small Android Demo to use TTS in different languages. I have a layout with two buttons, Spanish and English. Pressing the button triggers an utterance in the language selected.

不过,我不能改变的语言(setLanguage(区域设置区域设置))。我可以自己动手完成它,使用手机设置和更改TTS语言来美国,英国,意大利,德国等,但我的code似乎并没有工作。你能告诉我问题出在哪里?

However, I can't change the language (setLanguage (Locale locale)). I can do it by hand, using the phone settings and changing the TTS language to US, UK, Italian, German, etc, but my code doesn't seem to work. Could you tell me where the problem is?

感谢你!

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

}

推荐答案

从<一个href="https://web.archive.org/web/20120505124037/http://developer.android.com/resources/articles/tts.html" rel="nofollow">https://web.archive.org/web/20120505124037/http://developer.android.com/resources/articles/tts.html,你可能会想尝试这样的:

From https://web.archive.org/web/20120505124037/http://developer.android.com/resources/articles/tts.html, you may want to try this:

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

看起来很奇怪,但是这就是他们引用(不是 ES 像预期的那样)。

这篇关于设置语言TTS编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆