Android text to speech


In this Android tip, I am going to show you how to use TextToSpeech engine to speak the text out. TextToSpeech supports many languages such as English, French, Chinese, Japanese, Korean, German, Italian, and so on.
Before you can use TextToSpeech to speak out the text, you have to make sure that a TextToSpeech instance finished its initialization. To capture the signal of the initialization completion, you need to implement the TextToSpeech.OnInitListener interface and override the onInit(int status) method. The status argument can be SUCCESS or ERROR. If the initialization is done successfully, the status gets SUCCESS value. Otherwise, it gets ERROR value. When the initialization is successful, you can set attributes (e.g. speech rate, pitch, and language) to the TextToSpeech instance. It is a good idea to check whether the specified language is supported before using the TextToSpeech instance to speak the text. The setLanuage() method not only receive your input language, but also returns an integer value indicating that the language is supported or not. You will call the speak() method to speak out the text that you specified. When you are done using the TextToSpeech instance, you call the shutdown() method to release the resources used by the TextToSpeech engine.

Now to have an example application on using TextToSpeech engine to convert text to speech, you create a new Android project. Then in the activity_main.xml file, you add one EditText and one button as shown below.




<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.andtextspeech.MainActivity"
   android:orientation="vertical"
   >
  
   <EditText
     android:id="@+id/txt"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="Enter text"
      />

   <Button
     android:id="@+id/bt"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Speak it"
     android:onClick="speakIt"
     />

</LinearLayout>


Here is the code written in MainActivity class to create a TextToSpeech instance, receive initialization completion signal, and speak the input text in EditText.

package com.example.andtextspeech;
import java.util.Locale;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtInput;
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     btnSpeak=(Button)findViewById(R.id.bt);
     txtInput=(EditText)findViewById(R.id.txt);
     tts = new TextToSpeech(this, this);
   }
  
   public void speakIt(View view){
  
   String text = txtInput.getText().toString();
   if(tts!=null)
   tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
  
   }
  
   @Override
   public void onInit(int status) {
   // TODO Auto-generated method stub
   if (status == TextToSpeech.SUCCESS) {
   // Set pitch level    
   tts.setPitch(1.5f);
   // Set speech speed rate
   tts.setSpeechRate(1.5f);
   // Checking whether the specified language is supported
   int result = tts.setLanguage(Locale.UK);    
   if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
   Log.e("TTS", "The Language is not supported.");
   btnSpeak.setEnabled(false);

   } else {
   btnSpeak.setEnabled(true);
   Log.e("TTS", "The Language is supported.");
   }
   } else {
   Log.e("TTS", "Initilization Failed");

   }
  
   }

   @Override public void onDestroy() {
   // Don't forget to shutdown the TextToSpeech engine
   if (tts != null) {
   tts.stop();
   tts.shutdown();

   }
   super.onDestroy();

   }
}




Android-text-to-speech

Posted by: Dara | post date: 12-08-2014 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.