Wednesday, 24 April 2013

Text to speech converter

Steps for setting up.

1. create TextVoice.java class file in src folder in eclipse and copy bellow content
2. create textvoice.xml file in layout folder in eclipse and paste bellow content given for textvoice.xml file.
3. copy menifest file into AndroidMenifest.xml file into ur project








1. TextVoice.java file content
its just a class file in src folder in eclipse (if you use eclipse for code).

  1. package com.yadav.ruepshmusicrecorder;

  2. import java.util.Locale;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.speech.tts.TextToSpeech;
  6. import android.view.MenuItem.OnMenuItemClickListener;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;

  12. public class TextVoice extends Activity implements OnClickListener,
  13. OnMenuItemClickListener {
  14. TextToSpeech tts;
  15. EditText e;

  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. // TODO Auto-generated method stub
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.textvoice);

  21. e = (EditText) findViewById(R.id.etinput);

  22. Button b = (Button) findViewById(R.id.bTextToVoice);
  23. b.setOnClickListener(this);
  24. tts = new TextToSpeech(TextVoice.this,
  25. new TextToSpeech.OnInitListener() {

  26. @Override
  27. public void onInit(int status) {
  28. // TODO Auto-generated method stub
  29. if (status != TextToSpeech.ERROR) {
  30. tts.setLanguage(Locale.US);
  31. }
  32. }
  33. });
  34. }

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

  38. String toSpeak = e.getText().toString();
  39. tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
  40. }

  41. @Override
  42. public boolean onMenuItemClick(MenuItem item) {
  43. // TODO Auto-generated method stub
  44. return false;
  45. }

  46. @Override
  47. protected void onPause() {
  48. // TODO Auto-generated method stub
  49. if (tts != null) {
  50. tts.stop();
  51. tts.shutdown();
  52. super.onPause();
  53. }

  54. }
  55. }

2. textvoice.xml file content:


it should be in layout folder


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >

  6.     <TextView
  7.         android:id="@+id/textView1"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:text="Enter Text Below"
  11.         android:textSize="30sp" />

  12.     <EditText
  13.         android:id="@+id/etinput"
  14.         android:layout_width="match_parent"
  15.         android:layout_height="wrap_content"
  16.         android:layout_margin="10sp"
  17.         android:ems="10" >

  18.         <requestFocus />
  19.     </EditText>

  20.     <Button
  21.         android:id="@+id/bTextToVoice"
  22.         android:layout_width="match_parent"
  23.         android:layout_height="wrap_content"
  24.         android:layout_margin="10sp"
  25.         android:text="Text to voice" />

  26. </LinearLayout>

3. AndroidMenifest file content


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.yadav.ruepshmusicrecorder"
  4.     android:versionCode="1"
  5.     android:versionName="1.0" >

  6.     <uses-sdk
  7.         android:minSdkVersion="8"
  8.         android:targetSdkVersion="10" />

  9.     <application
  10.         android:allowBackup="true"
  11.         android:icon="@drawable/ic_launcher"
  12.         android:label="@string/app_name"
  13.         android:theme="@style/AppTheme" >
  14.         <activity
  15.             android:name=".TextVoice"
  16.             android:label="@string/app_name" >
  17.         </activity>
  18.     </application>

  19. </manifest>

Thats it u r done...............;
just build the project and press crtl+f11;
i assume that u know how to create and setup android application project;
enjoy...;