Judul : Android Text to Speech Simple App
link : Android Text to Speech Simple App
Android Text to Speech Simple App
In this tutorial i also explained changing the language type, pitch level and speed level.
1. Create a new project by going to File ⇒ New Android Project. and fill required details.
2. Implement your MainActivity class from TextToSpeech.OnInitListener
2. Implement your MainActivity class from TextToSpeech.OnInitListener
3. Now add below code to your MainActivity.java
package gracious.text_to_speech; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.util.Locale; public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{ private TextToSpeech mTextToSpeech; private Button mButton; private EditText mEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mTextToSpeech = new TextToSpeech(this, this); mButton = (Button) findViewById(R.id.button); mEditText = (EditText) findViewById(R.id.editText); // button on click event mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { String text = mEditText.getText().toString(); mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null); } }); } @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { int result = mTextToSpeech.setLanguage(Locale.US); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { //No action to be take place } else { mButton.setEnabled(true); String text = mEditText.getText().toString(); mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null); } } else { //There is some error } } @Override public void onDestroy() { // Don't forget to shutdown tts! if (mTextToSpeech != null) { mTextToSpeech.stop(); mTextToSpeech.shutdown(); } super.onDestroy(); } } |
4. Now add below code to your content_main.xmlfile:-
<?xml version="1.0" encoding="utf-8"?>
android:text="Speack out the Text" |
4. Add below code to your activity_main.xml
<?xml version="1.0" encoding="utf-8"?> android:layout_width="match_parent" |
That's it, Test your code and app now.
Changing Language
You can change language to speak by using setLanguage() function. Lot of languages are supported like Canada, French, Chinese, Germany etc.
mTextToSpeech.setLanguage(Locale.CHINESE); // To set as Chinese lang
Changing Pitch Rate
You can set speed pitch level by using setPitch() function. By default the value is 1.0 You can set lower values than 1.0 to decrease pitch level or greater values for increase pitch level.
mTextToSpeech.setPitch(0.5);
Changing Speed Rate
The speed rate can be set using setSpeechRate(). This also will take default of 1.0 value. You can double the speed rate by setting 2.0 or make half the speed level by setting 0.5
mTextToSpeech.setSpeechRate(2);
Demikianlah Artikel Android Text to Speech Simple App
Sekianlah artikel Android Text to Speech Simple App kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel Android Text to Speech Simple App dengan alamat link https://kokonghod.blogspot.com/2016/02/android-text-to-speech-simple-app.html
0 Response to "Android Text to Speech Simple App"
Post a Comment