Hello,
I'm making text to speech application.
I successfully implemented the application by referring some tutorials. And it works fine.
But those tutorials use only one class to implement text to speech application.
Now, I want to use two classes. One for main activity and another for text to speech.
I tried to implement it. But the problem is that the text to speech conversion is not working. I can't hear any voice output.
My codes are given below..
Main activity class is...
.........................................................
class for text to speech conversion is....
..........................................................................................
Please help me?
I'm making text to speech application.
I successfully implemented the application by referring some tutorials. And it works fine.
But those tutorials use only one class to implement text to speech application.
Now, I want to use two classes. One for main activity and another for text to speech.
I tried to implement it. But the problem is that the text to speech conversion is not working. I can't hear any voice output.
My codes are given below..
Main activity class is...
.........................................................
Code:
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
public class Tts_2Activity extends Activity {
Speak speak;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//final Speak speak;
speak=new Speak(this);
read();
}
public void read(){
try{
speak.spell("hai, testing");
}catch(Exception e){
new AlertDialog.Builder(this).setMessage(e.toString()).show();
}
}
}
..........................................................................................
Code:
import java.util.Locale;
import android.app.AlertDialog;
import android.content.Context;
import android.speech.tts.TextToSpeech;
public class Speak implements TextToSpeech.OnInitListener{
TextToSpeech tts;
Context context;
public Speak(Context act_context)
{
context =act_context;
tts = new TextToSpeech(context, this);
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
new AlertDialog.Builder(context).setMessage("This Language is not supported").show();
//Log.e("TTS", "This Language is not supported");
}
} else {
new AlertDialog.Builder(context).setMessage("This Language is not supported").show();
//Log.e("TTS", "Initialization Failed!");
}
}
public void spell(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
}
}
Please help me?