TTS 예제 프로그램입니다.
setLanguage() // 지역 선택
setPitch() // 낮거나 높은음을 일정하게 맞춰 주는 옵션
setSpeechRate() // 재생 속도
speak() // 재생
실행 화면
코드 사용 (xml)
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:id="@+id/et" android:text="There is something I want to talk to you about."/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_below="@+id/et" android:text="play" android:id="@+id/play"/> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rg_local" android:layout_marginTop="20dp" android:orientation="horizontal" android:layout_below="@id/play"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:id="@+id/rb_local1" android:text="us"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_local2" android:text="korea"/> </RadioGroup> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rg_pitch" android:layout_marginTop="20dp" android:orientation="horizontal" android:layout_below="@id/rg_local"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:id="@+id/rb_pitch1" android:text="pitch row"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_pitch2" android:checked="true" android:text="pitch normal"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_pitch3" android:text="pitch high"/> </RadioGroup> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rg_rate" android:layout_marginTop="20dp" android:orientation="horizontal" android:layout_below="@id/rg_pitch"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_rate1" android:text="x0.1"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_rate2" android:checked="true" android:text="x1"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_rate3" android:text="x2"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_rate4" android:text="x3"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_rate5" android:text="x4"/> </RadioGroup> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rg_queue" android:layout_marginTop="20dp" android:orientation="horizontal" android:layout_below="@id/rg_rate"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:id="@+id/rb_queue_add" android:text="QUEUE_ADD"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_queue_flush" android:text="QUEUE_FLUSH"/> </RadioGroup> </RelativeLayout>
코드 사용
import android.content.ComponentName; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.support.annotation.IdRes; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioGroup; import java.util.HashMap; import java.util.Locale; public class MainActivity extends AppCompatActivity { private TextToSpeech mTts = null; private Locale mLocale = Locale.US; private float mPitch = (float) 0; private float mRate = (float) 0; private int mQueue = TextToSpeech.QUEUE_ADD; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText et = (EditText) findViewById(R.id.et); Button play = (Button) findViewById(R.id.play); play.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(mTts != null) { if(et != null) { String text = et.getText().toString(); if(text.length() > 0) { setLanguage(mLocale); setPitch(mPitch); setSpeechRate(mRate); speak(text, 0); } } } } }); RadioGroup lay_local = (RadioGroup) findViewById(R.id.rg_local); RadioGroup lay_pitch = (RadioGroup) findViewById(R.id.rg_pitch); RadioGroup lay_rate = (RadioGroup) findViewById(R.id.rg_rate); RadioGroup lay_queue = (RadioGroup) findViewById(R.id.rg_queue); lay_local.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) { if(id == R.id.rb_local1) { mLocale = Locale.US; } else { mLocale = Locale.KOREA; } } }); lay_pitch.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) { if(id == R.id.rb_pitch1) { mPitch = (float) 0.1; } else if(id == R.id.rb_pitch2) { mPitch = 1; } else if(id == R.id.rb_pitch3) { mPitch = 2; } } }); lay_rate.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) { if(id == R.id.rb_rate1) { mRate = (float) 0.1; } else if(id == R.id.rb_rate2) { mRate = 1; } else if(id == R.id.rb_rate3) { mRate = 2; } else if(id == R.id.rb_rate4) { mRate = 3; } else if(id == R.id.rb_rate5) { mRate = 4; } } }); lay_queue.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) { if(id == R.id.rb_queue_add) { mQueue = TextToSpeech.QUEUE_ADD; } else { mQueue = TextToSpeech.QUEUE_FLUSH; } } }); init(); } @Override protected void onPause() { super.onPause(); if (mTts != null) { if(mTts.isSpeaking()) { mTts.stop(); } } } @Override public void onDestroy() { if (mTts != null) { if(mTts.isSpeaking()) { mTts.stop(); } mTts.shutdown(); } super.onDestroy(); } private void init() { mTts = new TextToSpeech(getBaseContext(), new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if(status == TextToSpeech.SUCCESS) { } else{ // todo: fail 시 처리 startActivity(getSettingActIntent()); } } }); } /** 언어 선택 */ public void setLanguage(Locale locale){ if(mTts!=null) mTts.setLanguage(locale); } public void setPitch(float value){ if(mTts!=null) mTts.setPitch(value); } /** 속도 선택 */ public void setSpeechRate(float value){ if(mTts!=null) mTts.setSpeechRate(value); } /** TTS 설정 으로 이동 */ public Intent getSettingActIntent(){ Intent intent = new Intent(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){ intent.setAction("com.android.settings.TTS_SETTINGS"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); }else { intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.TextToSpeechSettings")); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } return intent; } /** 재생 */ public void speak(String text, int resId){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ if(mTts != null) mTts.speak(text, mQueue, null, ""+resId); }else { HashMap<String, String> map = new HashMap<>(); map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, ""+resId); if(mTts != null) mTts.speak(text, mQueue, map); } } }
'Android' 카테고리의 다른 글
[안드로이드] org.apache.log4j.chainsaw.ControlPanel: can't find superclass or interface javax.swing.JPanel 오류 대응 (0) | 2017.04.25 |
---|---|
[안드로이드] SQL Like 검색 (0) | 2017.04.25 |
[안드로이드] android google map cluster example (0) | 2017.04.18 |
[안드로이드] Activity,Fragment 에서 Bundle 로 Object, ArrayList 넘기기 (1) | 2017.04.17 |
[안드로이드] sqlite Like 검색 예제 (0) | 2017.04.17 |