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
- 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 thisimport 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 thisimport 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;@Overridepublic 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();}});}@Overridepublic 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!!!
Thanks for this post ... simple and to the point.
ReplyDeletethanks.
ReplyDeleteThis is really an awesome article to implement Text to speech functionality.
ReplyDeletesorry, i became fool, its copy cat from http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html
ReplyDeleteyup... it says right on top that its a tweak of code sample. but they never gave easy steps to run just text to speech.
ReplyDeleteHi Dear ,
ReplyDeleteThanks 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);"
Hi Zahid,
ReplyDeleteI have a problem to import android.speech.tts.TextToSpeech. Why is that?
Hope anyone could tell me why...?
Wow really i like it you post because very good information for android development thanks for sharing post.
ReplyDeleteThe 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.
Google is one of the most popular search Engine which is taking a Important part In internet marketing field to generate more traffic.
ReplyDeleteAndroid app developers
Its a good knowledgeable resource and your explanation is easy to understood, so i would like to thank for creating this interesting blog.
ReplyDeleteAndroid developers
Excellent Blog. I really want to admire the quality of this post. Its really impressive too..
ReplyDeleteTop seo forums
You do not have the skills and workforce required for Android Development, you need to get someone else to do it.
ReplyDeleteReally your content is so informative. so please share some more content ..
ReplyDeleteAndroid training in Noida
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.
ReplyDeletebest android freelance jobs finding App
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your blog.
ReplyDeleteiPhone training courses in bangalore
iPhone training courses in bangalore
Find-out Best Android App Development Services in India, USA, UK, HongKong
ReplyDeleteOnline 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
ReplyDeleteGreat blog posting informative
ReplyDeletePiping Design Engineering course is a job oriented diploma course. Sanjary academy provide best piping design course in Hyderabad, India with job assistance.
best Piping Design Course
piping design course with placement
pipe stress analysis course in hyderabad
piping engineering course in hyderabad
piping stress analysis course
best Piping Design institute
best institute of Piping Design Course in India
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..
ReplyDeleteArtificial Intelligence Online Training
Java Online Training
AWS Online Training
Machine Learning Online Training
Data Science Online Training
DevOps Online Training
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.
ReplyDeleteFor More Info: Mobile App Development Company
Excellent content ,Thanks for sharing this .,
ReplyDeleteProduct owner certification