November 4, 2017
Android text to speech example source code TTS Dialog
Android Text To Speech complete example source code
Android Text To Speech with editText. Android hear voice from edittext text. Convert your text to voice. Below is the complete source code of Android Text To Speech or TTS.
package write your package name here; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.util.Locale; public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener { privateTextToSpeech tts; privateButton speak; privateEditText getTextToSpeak; @Override protectedvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tts = new TextToSpeech(this, this); getTextToSpeak = (EditText) findViewById(R.id.editTextT); speak = (Button) findViewById(R.id.speakOut); speak.setOnClickListener(new View.OnClickListener() { @Override publicvoidonClick(Viewview) { speakOutNow(); } }); } @Override publicvoidonInit(inttext) { if (text ==TextToSpeech.SUCCESS) { int language = tts.setLanguage(Locale.ENGLISH); //YOU CAN CHANGE YOUR LANGUAGE if (language ==TextToSpeech.LANG_MISSING_DATA || language ==TextToSpeech.LANG_NOT_SUPPORTED){ speak.setEnabled(true); speakOutNow(); //YOU CAN TRY REMOVE THIS LINE } else { //put a toast } } else{ } } privatevoidspeakOutNow (){ String text = getTextToSpeak.getText().toString(); tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); }
@Override public void onDestroy() { // Don't forget to shutdown tts! if (tts != null) { tts.stop(); tts.shutdown(); } super.onDestroy(); }
}
activity_main.xml source code for android text to speech (TTS)
<?xml version="1.0" encoding="utf-8" ?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#7fdfed" tools:context="write your package name here.MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Write your text" android:ems="10" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:layout_marginTop="100dp" android:id="@+id/editTextT" /> <Button android:text="speak out" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentStart="true" android:id="@+id/speakOut" /> <TextView android:text="ATTRACTION" android:textColor="#0088ff" android:textStyle="bold" android:textSize="40sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Android editText to speech(TTS) example with source code.
<?xml version="1.0" encoding="utf-8" ?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="utube.attraction.texttospeech">
<application
android:allowBackup=”true“
android:icon=”@mipmap/ic_launcher“
android:label=”@string/app_name“
android:supportsRtl=”true“
android:theme=”@style/AppTheme“>
<activity
android:name=”.MainActivity“>
<intent-filter>
<action
android:name=”android.intent.action.MAIN“ />
<category
android:name=”android.intent.category.LAUNCHER“ />
</intent-filter>
</activity>
</application>
</manifest>