Sunday 3 March 2013

Text To Speach Demo



Demo:


1)main.xml:
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Enter Text Here"
        android:textColor="#000000"
        android:textSize="15dp" />

    <EditText
        android:id="@+id/edt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btn"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Speak" />

</LinearLayout>



2)Activity:


package com.venky.testtospeach;

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 TestToSpeachActivity extends Activity {
      /** Called when the activity is first created. */

      EditText edt1;

      TextToSpeech tts;

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

            tts = new TextToSpeech(getApplicationContext(), new OnInitListener() {

                  @Override
                  public void onInit(int status) {
                        // TODO Auto-generated method stub

                        Toast.makeText(getApplicationContext(), "Ready to Speach",
                                    Toast.LENGTH_SHORT).show();
                  }
            });
            edt1 = (EditText) findViewById(R.id.edt1);

            Button btn = (Button) findViewById(R.id.btn);
            btn.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        String enteredText = edt1.getText().toString().trim();

                        tts.speak(enteredText, RESULT_OK, null);
                       
                        Toast.makeText(getApplicationContext(), "Speach is Progress",
                                    Toast.LENGTH_SHORT).show();
                  }
            });
      }
}



No comments:

Post a Comment