Tuesday, September 10, 2013

Android Text To Speech Example

In Android 1.6 (SDK API level 4) the text to speech (TTS) engine was introduced. We can use this API to
produce speech synthesis from within our applications, allowing the application to ‘‘talk’’ to your users.

 Before using the TTS engine, it’s good practice to confirm the language packs are installed or not, if not installed we must install the language pack.

Once you’ve confirmed the voice data is available, you need to create and initialize a new TextToSpeech
instance. Note that you cannot use the new Text To Speech object until initialization is complete.

 public void onInit(int status)
            {
               
                    if (status == TextToSpeech.SUCCESS)
                    {

                               // ready to Speak
                    }
          }

When Text To Speech has been initialized you can use the speak method to synthesize voice using the
default device audio output.

tts.speak("Text to Speak", TextToSpeech.QUEUE_FLUSH, null)


Android Text To Speech Example:


In the example we have an EditText , in which user enters the text and  when clicks on Button  we c all speakTheText() method to speak the entered Text.


main.xml


Android TextToSpeech


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D1FFFF"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_marginTop="120dp"
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="34dp"
        android:textStyle="bold"
        android:text="Text To Speech Tutorial" />

    <EditText
        android:id="@+id/editTextToSpeech"
         android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:hint="Enter the text to speak"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/buttonSpeak"
        android:layout_marginTop="30dp"
        android:textSize="25dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Speak Now" />
   
   
  </LinearLayout>







MainActivity.java


public class MainActivity extends Activity  implements TextToSpeech.OnInitListener
{
            EditText editText;
            Button buttonSpeak;
          
            TextToSpeech tts ;
           
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);
                       
                        tts = new TextToSpeech(this, this);
                       
                        editText =(EditText)findViewById(R.id.editTextToSpeech);
                        buttonSpeak =(Button)findViewById(R.id.buttonSpeak);
                       
           
                        buttonSpeak.setOnClickListener(new View.OnClickListener() {
                           
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                speakTheText();
                               
                            }
                        });
            }

           
           
           
            @Override
            public void onDestroy()
            {
                // Do Not forget to Stop the TTS Engine when you do not require it       
                    if (tts != null)
                    {
                        tts.stop();
                        tts.shutdown();
                    }
                    super.onDestroy();
            }

            public void onInit(int status)
            {
               
                    if (status == TextToSpeech.SUCCESS)
                    {

                            int result = tts.setLanguage(Locale.GERMAN);

                            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
                            {
                                   Toast.makeText(this, "This Language is not supported", Toast.LENGTH_LONG).show();
                            }
                            else
                            {
                                Toast.makeText(this, "Ready to Speak", Toast.LENGTH_LONG).show();
                                speakTheText();
                            }

                    }
                    else
                    {
                         Toast.makeText(this, "Can Not Speak", Toast.LENGTH_LONG).show();
                    }

            }

            private void speakTheText()
            {
                        String textToSpeak = editText.getText().toString();
                        tts.speak(textToSpeak, TextToSpeech.QUEUE_FLUSH, null);
            }


}







 

New Advance Topics:                   Android LiveWallpaer Tutorial
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swipe Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation
Android AlarmManager                 Android BootReceiver                       Vibrate Phone In a Desirable Pattern    
Developing for Different Screen Sizes           Showing Toast for Longer Time       Publishing your App
How to publish Android App on Google Play
Android TextWatcher                               Android ExpandableListView

 Beginning With Android
      Android : Introduction(What is Android)                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views


Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Advance Views
Android Spinner                                                                           Android GalleryView
Android TabWidget                                                                      Android ExpandableListView

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time


TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage

Working With Incoming Calls                                       DataBase :  Introduction of SQLiteDataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android


Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
How to Hide Title Bar In Android
How to show an Activity in Landscape or Portrait Mode only.
How to Set an Image as Wallpaper.






28 comments:

  1. wow cool application.........thax for this one.......!

    ReplyDelete
  2. can i Select more than one language usin Spinner contorl?

    ReplyDelete
  3. very good post, it was really informative thanks a lot for posting…
    Mobile App Development

    ReplyDelete
  4. In the event that you know where you can discover a phone to utilize incidentally, you won't be abandoned when such a pitiful opportunity arrives.Handy reparatur

    ReplyDelete
  5. Thank you so much for the post you do. I like your post and all you share with us is up to date and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. Howtodoninja

    ReplyDelete
  6. At the point when we talk about telephone then right away Android come in our brain, and we realize that Android stage is developing at a rushed rate around the world.Brighter Guide

    ReplyDelete
  7. I simply wanted to inform you about how much I actually appreciate all you’ve contributed to help increase the value of the lives of people in this subject matter. Through your own articles, we have gone via just a newbie to a professional in the area. It can be truly a honor to your initiatives. Thanks text to speech video creation

    ReplyDelete
  8. Excellent post. I was always checking this blog, and I’m impressed! Extremely useful info specially the last part, I care for such information a lot. I was exploring this particular info for a long time. Thanks to this blog my exploration has ended. android text auto reply

    ReplyDelete
  9. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. out of office text message

    ReplyDelete
  10. Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon! Auto Reply while Driving

    ReplyDelete
  11. Get cheap downloads, rankings and evaluations by any keyword and get more organic traffic in 22 lnaguages. AppRanky

    ReplyDelete
  12. Android is a framework developed by Google and Open Handset Alliance and is well known for creating smart yet easy to use Mobile Applications. Honeycomb is one of the latest versions to be launched in the market. app development company in south Africa

    ReplyDelete
  13. There is clearly a lot to know about this. I think you made various good points in features also. app development company in south Africa

    ReplyDelete
  14. There is clearly a lot to know about this. I think you made various good points in features also. web hosting

    ReplyDelete
  15. There is clearly a lot to know about this. I think you made various good points in features also. kiss918 apk

    ReplyDelete
  16. There is clearly a lot to know about this. I think you made various good points in features also. kiss918

    ReplyDelete
  17. Watch the strategies presented continue reading to discover and just listen how to carry out this amazing like you organize your company at the moment. educational mega888

    ReplyDelete
  18. Maximize your by how a large amount of gear are employed internationally and will often impart numerous memory using that your is also fighting that is a result from our team rrnside the twenty first centuries. daily deal livingsocial discount baltimore washington pussy888

    ReplyDelete
  19. There is clearly a lot to know about this. I think you made various good points in features also. Guesthouse booking

    ReplyDelete
  20. There is noticeably a bundle to find out about this. I assume you made sure nice factors in options also. mega888 apk download

    ReplyDelete
  21. This internet site is my breathing in, very great pattern and perfect content . color printer

    ReplyDelete
  22. Yours is a prime example of informative writing. I think my students could learn a lot from your writing style and your content. I may share this article with them. color copier

    ReplyDelete
  23. I saw a lot of website but I think this one contains something special in it in it XE88 apk download

    ReplyDelete
  24. There is clearly a lot to know about this. I think you made various good points in features also. Adhan and prayer time

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. Youre so cool! I dont suppose Ive learn something like this before. So good to find somebody with some unique ideas on this subject. realy thank you for beginning this up. this website is something that is needed on the net, somebody with somewhat originality. helpful job for bringing something new to the internet! reseller hosting

    ReplyDelete