8
我已經編寫了一個小型Android演示程序,以使用不同語言的TTS。我有兩個按鈕,西班牙語和英語的佈局。按下按鈕會以選定的語言觸發發言。以編程方式爲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();
}
}
謝謝費米,我也試過,但它似乎也沒有工作:S – Ignacio
我終於解決了這個問題!我不得不手動取消選中「文本到語音設置」下的「始終使用我的設置」複選框。 – Ignacio
@Ignacio在那裏我可以找到三星Galaxy S3 –