This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

Showing posts with label Text to Speech Example in Android. Show all posts
Showing posts with label Text to Speech Example in Android. Show all posts

Monday, 21 January 2013

Text to Speech Example in Android

Project Structure :


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/input_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="78dp"
        android:ems="10" />

    <TextView
        android:id="@+id/label_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="Enter Text to Speak"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/speak_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/label_text_view"
        android:layout_below="@id/input_edit_text"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="20dp"
        android:text="Speak" />

</RelativeLayout>
MainActivity.java
package com.rajeshvijayakumar.text2speech;

import java.util.Locale;

import android.app.Activity;
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;

public class MainActivity extends Activity implements OnInitListener, OnClickListener {

    private TextToSpeech mText2Speech;
    private EditText mInputEditText;
    private Button mSpeakButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mInputEditText = (EditText) findViewById(R.id.input_edit_text);
        mSpeakButton = (Button) findViewById(R.id.speak_button);
        mText2Speech = new TextToSpeech(MainActivity.this, MainActivity.this);
        mSpeakButton.setOnClickListener(this);

    }

    @Override
    public void onInit(int status) {
      
        if(status == TextToSpeech.SUCCESS) {
            mText2Speech.setLanguage(Locale.getDefault());
        }
      
    }

    @Override
    public void onClick(View v) {

        String speakText = mInputEditText.getText().toString();
        switch(v.getId()) {
        case R.id.speak_button :
                if(mText2Speech.isSpeaking()) {
                    mText2Speech.stop();
                    mText2Speech.shutdown();
                } else {
                    mText2Speech.speak(speakText, TextToSpeech.QUEUE_FLUSH, null);
                }
            break;
        }
    }
}

AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rajeshvijayakumar.text2speech"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.rajeshvijayakumar.text2speech.MainActivity"
            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>
Output :