Android Text To Speech Example-2

  • 0

Android Text To Speech Example-2

Category : Uncategorized

TextToSpeech class is responsible to convert text into speech. It provides a lot of methods to control the speech such as setSpeedRate(), setPitch() etc.

In this example, we are going to see the android texttospeech example with speed and pitch option.

activity_main.xml

Drag 2 textviews, 1 edittext, 1 spinner and 1 button for the layout. Now the activity_main.xml file will look like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <EditText
  7.         android:id=“@+id/editText1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignBaseline=“@+id/textView1”
  11.         android:layout_alignBottom=“@+id/textView1”
  12.         android:layout_alignParentRight=“true”
  13.         android:layout_marginRight=“58dp”
  14.         android:ems=“10” >
  15.         <requestFocus />
  16.     </EditText>
  17.     <Button
  18.         android:id=“@+id/button1”
  19.         android:layout_width=“wrap_content”
  20.         android:layout_height=“wrap_content”
  21.         android:layout_below=“@+id/editText1”
  22.         android:layout_centerHorizontal=“true”
  23.         android:layout_marginTop=“28dp”
  24.         android:text=“Speak” />
  25.     <TextView
  26.         android:id=“@+id/textView2”
  27.         android:layout_width=“wrap_content”
  28.         android:layout_height=“wrap_content”
  29.         android:layout_below=“@+id/button1”
  30.         android:layout_marginTop=“62dp”
  31.         android:layout_toLeftOf=“@+id/editText1”
  32.         android:text=“Speed:” />
  33.     <TextView
  34.         android:id=“@+id/textView1”
  35.         android:layout_width=“wrap_content”
  36.         android:layout_height=“wrap_content”
  37.         android:layout_alignLeft=“@+id/textView2”
  38.         android:layout_alignParentTop=“true”
  39.         android:layout_marginTop=“42dp”
  40.         android:text=“Text:” />
  41.     <Spinner
  42.         android:id=“@+id/spinner1”
  43.         android:layout_width=“200dp”
  44.         android:layout_height=“wrap_content”
  45.         android:layout_below=“@+id/button1”
  46.         android:layout_centerHorizontal=“true”
  47.         android:layout_marginTop=“43dp” />
  48. </RelativeLayout>

Activity class

Let’s see the code to speak the given text.

File: MainActivity.java
  1. package com.example.texttospeechspeed;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Locale;
  8. import android.speech.tts.TextToSpeech;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.AdapterView.OnItemSelectedListener;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.Spinner;
  17. import android.widget.Toast;
  18. public class MainActivity extends Activity implements
  19. TextToSpeech.OnInitListener,OnItemSelectedListener {
  20. /** Called when the activity is first created. */
  21. private TextToSpeech tts;
  22. private Button buttonSpeak;
  23. private EditText editText;
  24. private Spinner speedSpinner,pitchSpinner;
  25. private static String speed=“Normal”;
  26. @Override
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30. tts = new TextToSpeech(thisthis);
  31. buttonSpeak = (Button) findViewById(R.id.button1);
  32. editText = (EditText) findViewById(R.id.editText1);
  33. speedSpinner = (Spinner) findViewById(R.id.spinner1);
  34. //load data in spinner
  35. loadSpinnerData();
  36. speedSpinner.setOnItemSelectedListener(this);
  37. //button click event
  38. buttonSpeak.setOnClickListener(new View.OnClickListener() {
  39.     @Override
  40.     public void onClick(View arg0) {
  41.         setSpeed();
  42.         speakOut();
  43.     }
  44. });
  45. }
  46. @Override
  47. public void onInit(int status) {
  48. if (status == TextToSpeech.SUCCESS) {
  49.     int result = tts.setLanguage(Locale.US);
  50.     if (result == TextToSpeech.LANG_MISSING_DATA
  51.             || result == TextToSpeech.LANG_NOT_SUPPORTED) {
  52.         Log.e(“TTS”“This Language is not supported”);
  53.     } else {
  54.         buttonSpeak.setEnabled(true);
  55.         speakOut();
  56.     }
  57. else { Log.e(“TTS”“Initilization Failed!”);}
  58. }
  59. @Override
  60. public void onDestroy() {
  61. // Don’t forget to shutdown tts!
  62. if (tts != null) {
  63.     tts.stop();
  64.     tts.shutdown();
  65. }
  66. super.onDestroy();
  67. }
  68. private void setSpeed(){
  69.     if(speed.equals(“Very Slow”)){
  70.         tts.setSpeechRate(0.1f);
  71.     }
  72.     if(speed.equals(“Slow”)){
  73.         tts.setSpeechRate(0.5f);
  74.     }
  75.     if(speed.equals(“Normal”)){
  76.         tts.setSpeechRate(1.0f);//default 1.0
  77.     }
  78.     if(speed.equals(“Fast”)){
  79.         tts.setSpeechRate(1.5f);
  80.     }
  81.     if(speed.equals(“Very Fast”)){
  82.         tts.setSpeechRate(2.0f);
  83.     }
  84.     //for setting pitch you may call 
  85.     //tts.setPitch(1.0f);//default 1.0
  86. }
  87. private void speakOut() {
  88. String text = editText.getText().toString();
  89. tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
  90. }
  91. private void loadSpinnerData() {
  92.     //Data for speed Spinner
  93.     List<String> lables = new ArrayList<String>();
  94.     lables.add(“Very Slow”);
  95.     lables.add(“Slow”);
  96.     lables.add(“Normal”);
  97.     lables.add(“Fast”);
  98.     lables.add(“Very Fast”);
  99.     // Creating adapter for spinner
  100.     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
  101.     // Drop down layout style – list view with radio button
  102.     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  103.     // attaching data adapter to spinner
  104.     speedSpinner.setAdapter(dataAdapter);
  105.   }
  106. @Override
  107. public void onItemSelected(AdapterView<?> parent, View view, int position,
  108.         long id) {
  109.     // On selecting a spinner item
  110.     speed = parent.getItemAtPosition(position).toString();
  111.     Toast.makeText(parent.getContext(), “You selected: “ + speed,
  112.             Toast.LENGTH_LONG).show();
  113. }
  114. @Override
  115. public void onNothingSelected(AdapterView<?> arg0) {
  116. }
  117. @Override
  118. public boolean onCreateOptionsMenu(Menu menu) {
  119.         // Inflate the menu; this adds items to the action bar if it is present.
  120.         getMenuInflater().inflate(R.menu.activity_main, menu);
  121.         return true;
  122. }
  123. }

Leave a Reply