Reputation: 11
I would like to ask about the correction of my problem. I saw an article that teaches how to use text to speech android code but I followed the code and my AVD does not seems to respond. So I went for another code and type it in, the avd does not seems to respond too. Can someone help me to check the error?
package net.learn2develop.tts;
import java.util.Locale;
import java.util.Random;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
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;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity implements OnInitListener{
private int MY_DATA_CHECK_CODE=0;
private TextToSpeech tts;
private EditText inputText;
private Button speakButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText=(EditText)findViewById(R.id.editText1);
tts=new TextToSpeech(getApplicationContext(),
new TextToSpeech.OnInitListener(){
@Override
public void onInit(int status){
if(status!=TextToSpeech.ERROR){
tts.setLanguage(Locale.UK);}
}
});
}
@Override
public void onPause(){
if(tts!=null){
tts.stop();
tts.shutdown();
}
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void speakText(View view){
String toSpeak=inputText.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
tts.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.learn2develop.tts.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="188dp"
android:layout_marginRight="67dp"
android:onClick="speakText"
android:text="@string/text1"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="81dp"
android:ems="10">
<requestFocus/>
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="@string/write"
android:textAppearance="?android:attr/textAppearanceLarge"/>
Upvotes: 0
Views: 2288
Reputation: 69
There are few mistakes that I found :-
you do not need to implement onInitListener and override method onInit() because you are using it once and already overriding in OnCreate() method.
after onInit() in OnCreate() u need to call speakText() method.
speakText(inputText)
Upvotes: 0
Reputation: 3754
First mistake is that you haven't initialised your button in the onCreate
,nor have you given an OnClickListener to it. Another problem here is with your onInit
. When you're overriding it,try working around like this:
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.UK);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
button1.setEnabled(true);
speakText();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
and the entire code would look something like this:
public class MainActivity extends Activity implements
TextToSpeech.OnInitListener {
private TextToSpeech tts;
private Button speakButton;
private EditText inputText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputText = (EditText) findViewById(R.id.editText1);
tts = new TextToSpeech(this, this);
speakButton = (Button) findViewById(R.id.button1);
// button on click event
speakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
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) {
Log.e("TTS", "This Language is not supported");
} else {
speakButton.setEnabled(true);
speakText();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakText() {
String toSpeak = inputText.getText().toString();
tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
}
Upvotes: 2