Thursday 25 September 2014

Alphabet Teacher Text to speech


Alphabet teacher uses TTS api to convert text to speech.The text is taken from the database then converted to speech. To get the speech started the default speech engine has to be enabled in your device from settings.
Source code:
package com.example.voice;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
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.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener, OnInitListener {
Button button;
ImageView image;
private int currentimageindex =0;
private int[] IMAGE_DBS ={R.drawable.apple, R.drawable.basket, R.drawable.cat , R.drawable.dog, R.drawable.egg, R.drawable.fish, R.drawable.goat, R.drawable.home, R.drawable.icecream, R.drawable.jug, R.drawable.king, R.drawable.lamp, R.drawable.mat, R.drawable.nest, R.drawable.orange, R.drawable.potato, R.drawable.queen, R.drawable.rabbit, R.drawable.sun, R.drawable.taxi, R.drawable.umbrella, R.drawable.van, R.drawable.wheel, R.drawable.xylophone,R.drawable.yak, R.drawable.zoo};
private TextToSpeech myTTS;
private int i=0;
static String[] hello ={ "A for apple","B for basket","C for cat","D for dog","E for egg","F for fish","G for goat","H for home ","I for ice cream","J for jug","K for king","L for lamp","M for mat","N for nest ","O for orange","P for potato","Q for queen ","R for rabbit","S for sun","T for taxi","U for umbrella","V for van","W for wheel","X for xylophone","Y for yak","Z for zoo"};

private int MY_DATA_CHECK_CODE = 0;
//create the Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button speakButton = (Button)findViewById(R.id.speak);
image = (ImageView) findViewById(R.id.imageView1);
//listen for clicks
speakButton.setOnClickListener(this);
//speakButton.setText("");
//check for TTS data
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
}
//respond to button clicks
public void onClick(View v) {
//get the text entered
try {
image.setImageResource(IMAGE_DBS[currentimageindex]);
currentimageindex++;
System.out.println("array");
EditText enteredText = (EditText)findViewById(R.id.enter);
enteredText.setText(""+hello[i++]);
enteredText.setTextSize(30);
enteredText.setFocusableInTouchMode(true);
enteredText.setFocusable(true);
String words = enteredText.getText().toString();
speakWords(words);
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}}
//speak the user text
private void speakWords(String speech) {
//speak straight away
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
//act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
//the user has the necessary data - create the TTS
myTTS = new TextToSpeech(this, this);
}
else {
//no data - install it now
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
//setup TTS
public void onInit(int initStatus) {
//check for successful instantiation
if (initStatus == TextToSpeech.SUCCESS) {    if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
}
}
}
 

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. I admit, I have not been on this web page in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues. video voice to text

    ReplyDelete