Thursday, 24 January 2013

TextToSpeech

ScreenShots:

activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter text to speak"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/TexteditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/speakButton"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Speak" />

</LinearLayout>

MainActivity.java:

package com.ram.texttospeech;

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;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText edit;
TextToSpeech textToSpeech;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textToSpeech = new TextToSpeech(getApplicationContext(), new OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "TextToSpeech engine intialiazed", Toast.LENGTH_LONG).show();
}
});
edit = (EditText)findViewById(R.id.TexteditText);
Button speakButton  = (Button)findViewById(R.id.speakButton);
speakButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String text = edit.getText().toString();
textToSpeech.speak(text, RESULT_OK, null);
}
});
}

 
}