Wednesday 27 February 2013

text to speech using android


The Android text to speech engine still seems to be a pretty underused resource in Android apps. However, implementing it in your own applications is straightforward. There are a few potential issues and choices you need to consider, but for most purposes, the process is not a complex one. In this tutorial we jump around a bit within one Android Activity, but don’t worry, the complete code is listed at the end. The aim is to give you a clear idea of the what’s going on at each processing stage so that you can successfully use the function in any app.
open it in your IDE. Otherwise, create a new Android project. You can use the code in this tutorial with any Activity class. For demonstration, we will first create some user interface elements. Again, if you already have your own UI, you can use it instead.

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>

activity.java:

package com.test.testspeech;

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 TexttospeechActivity extends Activity {
    /** Called when the activity is first created. */
            EditText edit;
            TextToSpeech textToSpeech;

             @Override
             protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
             
              textToSpeech = new TextToSpeech(getApplicationContext(), new OnInitListener() {
              
               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() {
              
               public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String text = edit.getText().toString();
               
               
                textToSpeech.speak(text, RESULT_OK, null);
               
               }
              });
             
             }

}

out put:



No comments:

Post a Comment