Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text to speech(TTS)-Android

I am new to the android platform. Now I am working on TTS(Text to Speech).If I enter the text in a TextArea and I would like it to be converted to speech when i click the speak button.

Can anyone help me out?

like image 940
bharathi Avatar asked Jun 17 '10 04:06

bharathi


People also ask

What is TTS on my Android phone?

With text-to-speech, your device can convert text input and play audio aloud.


2 Answers

Text to speech is built into Android 1.6+. Here is a simple example of how to do it.

TextToSpeech tts = new TextToSpeech(this, this); tts.setLanguage(Locale.US); tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null); 

More info: http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html


Here are instructions on how to download sample code from the Android SDK Manager:

  1. Launch the Android SDK Manager.

    a. On Windows, double-click the SDK Manager.exe file at the root of the Android SDK directory.

    b. On Mac or Linux, open a terminal to the tools/ directory in the Android SDK, then execute android sdk.

  2. Expand the list of packages for the latest Android platform.

  3. Select and download Samples for SDK. When the download is complete, you can find the source code for all samples at this location:

/sdk/samples/android-version/

(i.e. \android-sdk-windows\samples\android-16\ApiDemos\src\com\example\android\apis\app\TextToSpeechActivity.java) 
like image 186
Quantumgeek Avatar answered Sep 27 '22 23:09

Quantumgeek


MainActivity.class

import java.util.Locale;  import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences.Editor; import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.EditText;  public class MainActivity extends Activity {      String text;     EditText et;     TextToSpeech tts;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          et=(EditText)findViewById(R.id.editText1);         tts=new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {              @Override             public void onInit(int status) {                 // TODO Auto-generated method stub                 if(status == TextToSpeech.SUCCESS){                     int result=tts.setLanguage(Locale.US);                     if(result==TextToSpeech.LANG_MISSING_DATA ||                             result==TextToSpeech.LANG_NOT_SUPPORTED){                         Log.e("error", "This Language is not supported");                     }                     else{                         ConvertTextToSpeech();                     }                 }                 else                     Log.e("error", "Initilization Failed!");             }         });       }      @Override     protected void onPause() {         // TODO Auto-generated method stub          if(tts != null){              tts.stop();             tts.shutdown();         }         super.onPause();     }      public void onClick(View v){          ConvertTextToSpeech();      }      private void ConvertTextToSpeech() {         // TODO Auto-generated method stub         text = et.getText().toString();         if(text==null||"".equals(text))         {             text = "Content not available";             tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);         }else             tts.speak(text+"is saved", TextToSpeech.QUEUE_FLUSH, null);     }  } 

activity_main.xml

<RelativeLayout 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"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context=".MainActivity" >      <Button         android:id="@+id/button1"         style="?android:attr/buttonStyleSmall"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentTop="true"         android:layout_centerHorizontal="true"         android:layout_marginTop="177dp"         android:onClick="onClick"         android:text="Button" />      <EditText         android:id="@+id/editText1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignBottom="@+id/button1"         android:layout_centerHorizontal="true"         android:layout_marginBottom="81dp"         android:ems="10" >          <requestFocus />     </EditText>  </RelativeLayout> 
like image 21
Sifat Ifty Avatar answered Sep 27 '22 23:09

Sifat Ifty