<MainActivity.java>
import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener;
@Override protected void onCreate(Bundle savedInstanceState) {
...
mTTS = new TextToSpeech(this, this);
//버튼 눌렀을때 텍스트 읽어주기. mMic.setTag(mData.get(position).a_part); mMic.setOnClickListener(new OnClickListener(){
@Override public void onClick(View v) { // TODO Auto-generated method stub ImageButton btn = (ImageButton)v; btn.setSelected(!btn.isSelected()); if(btn.isSelected() == true) //playing a sentence { String text = (String)btn.getTag(); if(text.length() > 0) { mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null); } } else { if(mTTS != null && mTTS.isSpeaking()) { mTTS.stop(); } } }});
}
@Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); if(mTTS.isSpeaking() == true) mTTS.stop(); }
@Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if(mTTS != null) { if(mTTS.isSpeaking() == true) mTTS.stop(); mTTS.shutdown(); } }
@Override public void onInit(int status) { // Text to Speech // TODO Auto-generated method stub if (status == TextToSpeech.SUCCESS) { mTTS.setLanguage(Locale.US); mTTS.setSpeechRate(0.7f); //1.0f가 일반적인 속도이며, 2.0f는 두배 빠른 속도이다. } } |