
我已经编写了一个小型的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. */@OverrIDepublic 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"); }}@OverrIDeprotected 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");看起来很奇怪,但这就是他们所引用的(不像人们期望的那样).
总结以上是内存溢出为你收集整理的android – 以编程方式设置TTS的语言?全部内容,希望文章能够帮你解决android – 以编程方式设置TTS的语言?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)