AdSense

AdSense3

Friday 16 October 2015

Android TextToSpeech Example

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.   
  7.     <EditText  
  8.         android:id="@+id/editText1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignBaseline="@+id/textView1"  
  12.         android:layout_alignBottom="@+id/textView1"  
  13.         android:layout_alignParentRight="true"  
  14.         android:layout_marginRight="58dp"  
  15.         android:ems="10" >  
  16.   
  17.         <requestFocus />  
  18.     </EditText>  
  19.   
  20.     <Button  
  21.         android:id="@+id/button1"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_below="@+id/editText1"  
  25.         android:layout_centerHorizontal="true"  
  26.         android:layout_marginTop="28dp"  
  27.         android:text="Speak" />  
  28.   
  29.     <TextView  
  30.         android:id="@+id/textView2"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_below="@+id/button1"  
  34.         android:layout_marginTop="62dp"  
  35.         android:layout_toLeftOf="@+id/editText1"  
  36.         android:text="Speed:" />  
  37.   
  38.     <TextView  
  39.         android:id="@+id/textView1"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:layout_alignLeft="@+id/textView2"  
  43.         android:layout_alignParentTop="true"  
  44.         android:layout_marginTop="42dp"  
  45.         android:text="Text:" />  
  46.   
  47.     <Spinner  
  48.         android:id="@+id/spinner1"  
  49.         android:layout_width="200dp"  
  50.         android:layout_height="wrap_content"  
  51.         android:layout_below="@+id/button1"  
  52.         android:layout_centerHorizontal="true"  
  53.         android:layout_marginTop="43dp" />  
  54.   
  55. </RelativeLayout>  

Activity class

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

You need to run it on the Real Device (e.g. Mobile) to test the application.

No comments:

Post a Comment