Skip to main content

Text To Speech Android App.

                                                                                                                                                                     Before starting  example using Android SDK, you have to make sure that you have setup your Android development environment properly in your system and you you have knowledge with Eclipse IDE.

The first step is to create a simple Android Application using Eclipse IDE. Follow the option File -> New -> Project and finally select Android New Application wizard from the wizard list.
                                                          Photo

        Create blank activity with MainActivity


      

       
This Android app.  allows you convert your text into voice. Android provides TextToSpeech class for this purpose. In order to use this class, you need to instantiate an object of this class and also specify the initListener. Its syntax is given below:

private EditText write;
ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
   @Override
   public void onInit(int status) {
   }
}
);   

In this listener , you have to specify the properties for TextToSpeech object , such as its language ,pitch e.t.c. Language can be set by calling setLanguage() method. Its syntax is given below:

ttobj.setLanguage(Locale.UK);
following are the steps that demonstrates the working procedure of this app:

1.You will use Eclipse IDE to create an Android application and name it as TextToSpeech under a package com.example.texttospeech. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.

2.Modify src/MainActivity.java file to add TextToSpeech code

3.Modify layout XML file res/layout/activity_main.xml

4.Modify res/values/string.xml file.

5.Run the application and choose a running android device and install the application on it and verify the results.

here is the content of src/com.example.texttospeech/MainActivity.java.....

package com.example.texttospeech;

import java.util.Locale;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

   TextToSpeech ttobj;
   private EditText write;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      write = (EditText)findViewById(R.id.editText1);
      ttobj=new TextToSpeech(getApplicationContext(),
      new TextToSpeech.OnInitListener() {
      @Override
      public void onInit(int status) {
         if(status != TextToSpeech.ERROR){
             ttobj.setLanguage(Locale.UK);
            }                                                                                                                        
         }
      });
   }
   @Override
   public void onPause(){
      if(ttobj !=null){
         ttobj.stop();
         ttobj.shutdown();
      }
      super.onPause();
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
   public void speakText(View view){
      String toSpeak = write.getText().toString();
      Toast.makeText(getApplicationContext(), toSpeak,
      Toast.LENGTH_SHORT).show();
      ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

   }
}



     Here is the content of 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"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignParentRight="true"
      android:layout_marginBottom="188dp"
      android:layout_marginRight="67dp"
      android:onClick="speakText"
      android:text="@string/text1" />

   <EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/button1"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="81dp"
      android:ems="10" >
      <requestFocus />
   </EditText>

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="20dp"
      android:text="@string/write"
      android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout
Here is the content of Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">TextToSpeech</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="text1">Text to Speech</string>
   <string name="write">Write Text</string>
</resources>



    Here is the content of AndroidManifest.xml:-



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.texttospeech"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" />

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name="com.example.texttospeech.MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
           
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

            here are the screen shots of the running application.






Now just type some text in the field and click on the text to speech button below. A notification would appear and text will be spoken. It is shown in the image below:

                                             

Now type something else and repeat the step again with different locale. You will again hear sound. This is shown below:








Comments

Popular posts from this blog

Home automation using Arduino Board

Today’s homes require sophistication control in its different gadgets which are basically electronic appliances. This has revolutionized the area of home automation with respect to an increased level of affordability and simplicity through the integration of home appliances with smart phone and tablet connectivity. Smart phones are already feature-perfect and can be made to communicate to any other devices in an adhoc network with a connectivity options like Bluetooth . With the advent of mobile phones, Mobile applications development has seen a major outbreak. Utilizing the opportunity of automating tasks for a smart home, mobile phone commonly found in normal household can be joined in a temporary network inside a home with the electronic equipments. Android, by Google Inc. provides the platform for the development of the mobile applications for the Android devices . Home automation system is a mobile application developed using Android targeting its vast market which will be benefi...

Tips for Building Android Mobile and Tablet Apps

1. Check Popular Apps Before you actually start writing your own apps, you definitely want to resear ch the market if there are already exisiting apps providing the planned features. If there are, then take notes about what they provide and ask yourself if you are confident in doing better. 2. Model After Existing Sample Codes If you installed the Android SDK, there are several great sample projects inside sdk/extras/android/support/samples showcasing almost all the user interface examples and major functionalities, which you can use as templates to build and modify for your apps. 3. Avoid Undocumented APIs Some code worked at first but after a few Android SDK updates, it stopped working. 4. Layout for Device Orientation and Various Screen Sizes Since devices have embedded orientation sensors and they are usually defaulted to automatic mode, we need to account for that to provide a better user experience.