Text To Speech in Android

Android provides Speech package that makes Text to Speech conversion very easy. 

android.speech.tts.TextToSpeach class provides necessary methods to the trick. 
At the start we need to implement the TextToSpeech.OnInitListener to do the initialization. During initialization we can set the Locale, Audio pitch rate, audio speed.
setLanguage() method is used to set the language.
Example: setLanguage(Locale.US);

TextToSpeech.LANG_MISSING_DATA indicates that language data is missing.
TextToSpeech.LANG_NOT_SUPPORTED indicates that language is not supported.
We check all these conditions during initialization.

speak() method in the TextToSpeech class is used to convert text to speech. Here we can set different types of Queue modes. TextToSpeech.QUEUE_FLUSH will flush the playback queue by replacing the old entries with new entry. TextToSpech.QUEUQ_ADD will add the new entry at the end of the playback queue.

 

It is a good practice to release the resources used by the TextToSpeech engine by calling shutdown() method. Normally we do call shutdown() method inside onDestroy() of the activity.

In the example application I have created a Text field to get the input text from user and a control button to initiate the Text To Speech conversion.

Layout file:
tts_layout.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"
    tools:context=".TextToSpeechActivity" >

    <EditText
        android:id="@+id/inputText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="28dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/inputText"
        android:layout_below="@+id/inputText"
        android:layout_marginTop="14dp"
        android:text="Speak" />

</RelativeLayout>
Activity class:
TextToSpeechActivity.java
package com.example.texttospeech;

import java.util.Locale;

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

/**
 * Reads the input value from the user and converts into voice.
 * 
 * @author Prabu
 * @since Aug 09 2013
 * @version 1.0
 * 
 */
public class TextToSpeechActivity extends Activity implements
  TextToSpeech.OnInitListener {
 private TextToSpeech textToSpeech;
 private Button button;
 private EditText inputText;

 /**
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tts_layout);
  button = (Button) findViewById(R.id.button1);
  inputText = (EditText) findViewById(R.id.inputText);
  textToSpeech = new TextToSpeech(this, this);
  button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {
    convertTextToSpeech();
   }

  });
  convertTextToSpeech();
 }

 /**
  * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
  */
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.tts_layout, menu);
  return true;
 }

 /**
  * a callback to be invoked indicating the completion of the TextToSpeech
  * engine initialization.
  * 
  * @see android.speech.tts.TextToSpeech.OnInitListener#onInit(int)
  */
 @Override
 public void onInit(int status) {
  if (status == TextToSpeech.SUCCESS) {
   int result = textToSpeech.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!");
  }
 }

 /**
  * Releases the resources used by the TextToSpeech engine. It is good
  * practice for instance to call this method in the onDestroy() method of an
  * Activity so the TextToSpeech engine can be cleanly stopped.
  * 
  * @see android.app.Activity#onDestroy()
  */
 @Override
 public void onDestroy() {
  textToSpeech.shutdown();
super.onDestroy(); } /** * Speaks the string using the specified queuing strategy and speech * parameters. */ private void convertTextToSpeech() { String text = inputText.getText().toString(); if (null == text || "".equals(text)) { text = "Please give some input."; } textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null); } }
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="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.texttospeech.TextToSpeechActivity"
            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>
 
Advertisements

Solutions for common Android development problems – Tutorial

1. Solving typical Android development problems

Things are not always working as they should during your Android development. This section gives an overview over typical problems and how to solve them.

1.1. Clean Project

Several users report that they get the following errors:

  1. Project … is missing required source folder: ‘gen’

  2. The project could not be built until build path errors are resolved.

  3. Unable to open class file R.java.

To solve any of these errors, go to the project menu and select ProjectClean.

If you have library projects, make sure to clean them too.

1.2. android.content.ActivityNotFoundException

During start or during execution of your application, your application stops. In the LogCat view you see the message:android.content.ActivityNotFoundException : Unable to find explicit activity class; have you declared this activity in your AndroidManifest.xml?

Check in this case if you declared your activity in the AndroidManifest.xml file.

1.3. Problems with Android Debug Bridge (adb)

The communication with the emulator or your Android device might have problems. This communication is handled by the Android Debug Bridge (adb).

Eclipse allows you to reset the adb in case this causes problems. Select therefore the DDMS perspective via WindowOpen PerspectiveOther…DDMS

To restart the adb, select the “Reset adb” in the Device View.

1.4. LogCat

The LogCat view shows you the log messages of your Android device and helps you to analyze problems. For example Java exceptions in your program would be shown here. To open this view, select WindowShow ViewOther…AndroidLogCat.

1.5. Java7

If Android complains that you cannot use Java7 select your right-click on your project and select the Java Compiler entry. Ensure that Java 1.5 or Java 1.6 is used. You may have to select the Enable project specific settings checkbox.

Java compiler settings

1.6. Eclipse reports file from R.java as missing

Sometimes Eclipse complains that a file, e.g. R.layout.main cannot be found. Check in your source code that you did not importandroid.R. An android.R import will prevent Eclipse from finding your R file.

2. Solving not so typical Android development problems

This sections lists problem which happen sometimes but are not that frequently as the problems from the previous section.

2.1. java.lang.IllegalArgumentException: Binary XML file Duplicate id

Caused by an attempt to load the same fragment twice. The second time it is added to the FragmentManager this exception is thrown.

Solution: ensure that each fragment is only added once to an activity.

2.2. Android editor not opened

Android provides nice editors to edit Android resource files, unfortunately these editor are not always automatically used due to bugs in the ADT. If that happens, you can open this editor manually. Right-click on your menu file and select Open withAndroid Menu Editor.

2.3. Emulator does not start

If your emulator does not start, make sure that the android-sdk version is in a path without any spaces in the path name.

2.4. Timeout during deployment

If you face timeout issues during deployment you can increase the default timeout in the Eclipse preferences. Select WindowPreferencesAndroidDDMS and increase the ADB connection timeout (in ms) value.

2.5. Installation failed due to insufficient storage

Sometimes the emulator will refuse to install an application with the error message: INSTALL_FAILED_INSUFFICIENT_STORAGE.

An Android virtual device provides by default only 64M for the storaging Android applications. You can clean your installed application by restarting the emulator and selecting the Wipe user data flag.

Alternatively you can set the data partition size. If you press edit on the AVD, you can set the Ideal size of data partitionproperty via the New button.

Setting the Ideal size of data partition for the ADV

2.6. Debug Certificate expired

If you get the error message Debug Certificate expired switch to the folder which contains the Android AVD, e.g. .android under Linux and delete the debug.keystore file. This file is only valid for a year and if not present, Eclipse will regenerate the password.

2.7. Error message for @Override

The @Override annotation was introduced in Java 1.6. If you receive an error message for @Override, change the Java compiler level to Java 1.6. To do this, right-click on the project, select PropertiesJava Compiler Compiler compliance level and select1.6 in the drop-down box.

2.8. Missing Imports

Java requires that classes which are not part of the standard Java Language are either fully qualified or declared via imports.

If you see an error message with the XX cannot be resolved to a variable text, right-click in your Editor and select SourceOrganize Imports to important required packages.

How to Check Your Own Mobile Number : Airtel, Aircel, Docomo, Idea,etc

Whenever you get a new SIM card, it takes time for you to by heart your own mobile number. Sometimes,you might even forget your own mobile number due to several confusions and you always give a call to your friends or relatives mobile so that you can have a look at your number,its a long process, isn’t it? Well, now on, you can just use the codes in this tutorial to Check your own mobile number for major network providers such as the below given ones, we will actually use some codes that’ll show up your mobile number without wasting much time.

  1. Airtel
  2. BSNL
  3. Aircel
  4. Idea
  5. Reliance
  6. Tata Docomo
  7. Vodafone
  8. Videocon
  9. Virgin

You just have to open up your phone’s Dialer, type these codes and finally you need to hit the call button or sometimes you just need to type it. You’ll see that a small window pops up giving you the required details (Some carriers ask you to confirm it, just hit “OK” to proceed) . And the best part is that it works even with zero balance as its completely free, even better than calling others to check your mobile number because even that requires currency!

Check Your Airtel Mobile Number-

  • *121*9#
  • *140*1600#
  • *400*2*1*10#
  • *140*175
  •  *282#  *141*123#

Check Your Aircel Mobile Number-

  • *122*131#
  • *888#
  • *1#
  • *234*4#
  • *131#

Check Your BSNL Mobile Number-

  • *1#
  • *99#

Check Your Idea Mobile Number-

  • *147#
  • *1#
  • *789#
  • *131*1#
  • *100#

Check Your Reliance Mobile Number-

  •  *1#
  • *111#

Check Your Tata Docomo Mobile Number-

  • *1#
  • *580#
  • *124#

Check Your Vodafone Mobile Number-

  • *111*2#
  • *555#
  • *555*0#
  • *777*0#
  • *131*0#

Check Your Videocon Mobile Number-

  • *1#

Check Your Virgin Mobile Number-

  • *1#

Get Notification Everytime Someone Unfriends you on FB

Users on Facebook are increasing Day by Day. We all tend to Spend Time on Facebook. We like to be friends with as many people as can. On an Avg Each user have 100-150 People in their Friend list. This number may be quite low, because many people now a days likes to be friend with unknown people too.

Though There is a limit of having 5.000 Friends on Facebook for each user. Not many people would reach this milestone. Facebook Notifies you when you send friend request to someone and they accept it. They also notifies you if you have any New Friend request from others but 1 thing that Facebook don’t do is that you will not be able to see Who Unfriend you and when. 

It Remains Mystery, You will not know until one day you find that person in your friendlist by his/her name. But Thanks to Extension Developers we have Chrome Extension that Notifies you if someone Unfriends you. Let us see some steps so that next time if someone removes you from thier friend list you will get a notification.

Step 1:

Download and Install  Unfriend Notify on Facebook Free Extension in Chrome.

Step 2:
Simply Click Here to go to  the Extension in Chrome Webstore and Click on Free or Add to Chrome Button then click Add in the Popup.
 
Step 3:
The Extension will now Start Downloading and you will the then see its been installed on your Chrome Browser. A new page will open, there just click on Activate.
 
Step 4: 
Now to see if the extension is been added successfully, go to your Timeline by clicking on your Name on your facebook. Click on Friends, You will see a new tab with name “Lost Friends” added there.