Friday, June 28, 2013

Android's Text to Speech Tutorial

This is a sample activity which shows How to use Android’s Text to Speech capabilities. Text to Speech was introduced in Android 1.6, so when you create your new Android project make sure your minimum required SDK is set to Android 1.6 (or API level 4).

Basic description of algorithm in step by step form:
1.) Create a Project TextToSpeech.
2.) Put the following code snippet in res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     
<EditText android:id="@+id/input_text"
    android:text=""
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

<Button android:id="@+id/speak_button"
    android:text="Speak to me"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
</LinearLayout>


Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MyTextToSpeech. Enter following information:
Project name: TextToSpeech
Build Target: Android APIs 2.2
Application name: TextToSpeech
Package name: com.app.texttopeech
Create Activity: TextToSpeech

Description: https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F4.bp.blogspot.com%2F-kftQItW2Zws%2FUc3w6BSLAbI%2FAAAAAAAABF4%2FC99eCJuAVa0%2Fs300%2Fmyblog.jpg&container=blogger&gadget=a&rewriteMime=image%2F*
Put the following code in src/com.app.texttopeech/TextToSpeech

package com.app.TextToSpeech;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TextToSpeech extends Activity implements OnInitListener{
    /** Called when the activity is first created. */
    private int MY_DATA_CHECK_CODE = 0;

    private TextToSpeech tts;

    private EditText inputText;
    private Button speakButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        inputText = (EditText) findViewById(R.id.input_text);
        speakButton = (Button) findViewById(R.id.speak_button);

        speakButton.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
           String text = inputText.getText().toString();
           if (text!=null && text.length()>0) {
            Toast.makeText(MyTextToSpeech.this, "Saying: " + text, Toast.LENGTH_LONG).show();
            tts.speak(text, TextToSpeech.QUEUE_ADD, null);
           }
            }
        });
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
       }

       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            tts = new TextToSpeech(this, this);
        }
        else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
     }
       }

    @Override
    public void onInit(int status) {       
      if (status == TextToSpeech.SUCCESS) {
        Toast.makeText(MyTextToSpeech.this, "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
      }
      else if (status == TextToSpeech.ERROR) {
        Toast.makeText(MyTextToSpeech.this, "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
      }
        }
}



Run the Application
Output –The final output:
Description: https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F4.bp.blogspot.com%2F-3kvi7JB4FsI%2FUc3yLzKlBXI%2FAAAAAAAABGI%2Ft_0i-rK-LrU%2Fs300%2Fmyblog2.jpg&container=blogger&gadget=a&rewriteMime=image%2F*

Description: https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-AogcBFLXuUA%2FUc3yQISZdtI%2FAAAAAAAABGQ%2FEsA6cHMoheo%2Fs300%2Fmyblog3.jpg&container=blogger&gadget=a&rewriteMime=image%2F*


No comments:

Post a Comment