Wednesday, September 1, 2010

Tutorial to develop text to speech app on Android




This tutorial will show how to develop simple Text to speech application on
android(tested on htc droid incredible).  It assumes that you have already
managed to develop hello world(or default project ) in Eclipse per http://android-java.blogspot.com/

  • Step 1 - Create default android project in Eclipse. Run it to make
sure it works.

  • Step 2- Replace below code in your main(activity class). You may need to change package and class name.(This code is tweak of sdk sample - ie its simplified )

  

package com.test;//change this
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.test.R; //change this
import java.util.Locale;
import java.util.Random;
public class TestAct extends Activity implements TextToSpeech.OnInitListener {
private static final String TAG = "TextToSpeechDemo";
private TextToSpeech mTts;
private Button mAgainButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize text-to-speech. This is an asynchronous operation.
// The OnInitListener (second argument) is called after initialization completes.
mTts = new TextToSpeech(this,
this //TextToSpeech.OnInitListener
);
mAgainButton = (Button) findViewById(R.id.again_button);
mAgainButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sayHello();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
// Implements TextToSpeech.OnInitListener.
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
} else {
// Check the documentation for other possible result codes.
// For example, the language may be available for the locale,
// but not for the specified country and variant.
// The TTS engine has been successfully initialized.
// Allow the user to press the button for the app to speak again.
mAgainButton.setEnabled(true);
// Greet the user.
sayHello();
}
} else {
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
private static final Random RANDOM = new Random();
private static final String[] HELLOS = {
"Hello World",
"This is Text to speech demo by Zahid Shaikh"
};
int i =0;
private void sayHello() {
// Select a random hello.
int helloLength = HELLOS.length;
String hello = HELLOS[i];
i++;
if(i == helloLength) i =0;
mTts.speak(hello,
TextToSpeech.QUEUE_FLUSH, // Drop allpending entries in the playback queue.
null);
}
}
  • Step 3 -   Change res/main/layout.xml (or equivalent)
<linearlayout android:background="#ff00ff" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
    <button android:enabled="false" android:id="@+id/again_button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:minlines="3" android:text="@string/again" type="submit"> </button>


</linearlayout>

  • Step 4 - change res/values/string.xml
<resources>
    <string name="hello">hi world</string>
    <string name="app_name">test</string>
<string name="again">Again</string>
<string name="red">red</string>

<string name="white">#FF00FF</string>
</resources>

  • Run your code - and press on the again button!!! 

21 comments:

  1. Thanks for this post ... simple and to the point.

    ReplyDelete
  2. This is really an awesome article to implement Text to speech functionality.

    ReplyDelete
  3. sorry, i became fool, its copy cat from http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html

    ReplyDelete
  4. yup... it says right on top that its a tweak of code sample. but they never gave easy steps to run just text to speech.

    ReplyDelete
  5. Hi Dear ,
    Thanks for your this Tutorials which to help me.
    it was good and I appreciat this but there is some error are facing me but can not solve it properly Error are enclosed bellow.

    "mAgainButton = (Button) findViewById(R.id.again_button);"

    ReplyDelete
  6. Hi Zahid,

    I have a problem to import android.speech.tts.TextToSpeech. Why is that?
    Hope anyone could tell me why...?

    ReplyDelete
  7. Wow really i like it you post because very good information for android development thanks for sharing post.

    The usage of this hand held speech device at home or office does not seem to be a problem but the real mess is created when cell phone is being used while driving. Taking calls while driving is risky but more risk shows up when one is texting while driving. When people TWD, they take almost 30 seconds to write a single phrase or around which is more than enough to indulge in distracted driving.

    ReplyDelete
  8. Google is one of the most popular search Engine which is taking a Important part In internet marketing field to generate more traffic.
    Android app developers

    ReplyDelete
  9. Its a good knowledgeable resource and your explanation is easy to understood, so i would like to thank for creating this interesting blog.

    Android developers

    ReplyDelete
  10. Excellent Blog. I really want to admire the quality of this post. Its really impressive too..
    Top seo forums

    ReplyDelete
  11. You do not have the skills and workforce required for Android Development, you need to get someone else to do it.

    ReplyDelete
  12. Really your content is so informative. so please share some more content ..
    Android training in Noida

    ReplyDelete
  13. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    best android freelance jobs finding App

    ReplyDelete
  14. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your blog.
    iPhone training courses in bangalore
    iPhone training courses in bangalore

    ReplyDelete
  15. Online course. Learn how to develop a recurring subscription food delivery app like doordash, Blue Apron, hellofresh with core functionalities from scratch. It can be for any subscription bases business. You don't need to learn everything about HTML, CSS, Javascript or Ruby on Rails to just create a web application. With step-by-step code along lessons, you can learn the important things to build an app for your coding project or even your own startup. It can get you building something REAL quickly and efficiently. (Ideal for entrepreneurs and founders) https://plantoost.com/courses/build-a-meal-kit-delivery-web-app-ruby-on-rails-stripe

    ReplyDelete
  16. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 

    Artificial Intelligence Online Training
    Java Online Training
    AWS Online Training
    Machine Learning Online Training
    Data Science Online Training
    DevOps Online Training

    ReplyDelete
  17. Ajath has an amazing history as a mobile application development company in Dubai with clients over the world. We have just taken care of various mobile app structure projects and have utilized development and creativity to think of notable apps that have helped in accomplishing business objectives for our highly esteemed clients.
    For More Info: Mobile App Development Company

    ReplyDelete
  18. Excellent content ,Thanks for sharing this .,
    Product owner certification

    ReplyDelete