Text To Speech
Text To Speech
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110sp"
android:layout_marginTop="100sp"
android:text="TextToSpeech"
android:textColor="#009688"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter text : "
android:textSize="18sp"
android:layout_marginTop="80sp"
android:layout_marginLeft="50sp"
android:textColor="#673AB7"
android:textStyle="bold"/>
<EditText
android:id="@+id/text"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_marginTop="-40sp"
android:layout_marginLeft="150sp"
android:inputType="textMultiLine"/>
<Button
android:id="@+id/button"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:text="Speak ->"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="70sp"
android:layout_marginLeft="100sp"/>
</LinearLayout>
MainActivity.java
import java.util.Locale;
TextToSpeech t1;
EditText e1;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = findViewById(R.id.text);
b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = e1.getText().toString();
t1.setPitch((float)1.0);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD,null);
}
});
}
public void onDestroy()
{
if(t1 != null) {
t1.stop();
t1.shutdown();
}
super.onDestroy();
}
}