I am making a simple application where when the user clicks on the button, background voice says "hello world". I have gone through tutorials but their problem is they all use text field to write the text by the user and then it is executed by the button. But what I want is the user to simply click on the button and voice should say "hello world".
Asked
Active
Viewed 2,465 times
1
-
What do you mean "embedded"? – OneCricketeer Apr 07 '17 at 17:38
-
Solution http://stackoverflow.com/questions/3058919/text-to-speechtts-android – Vinayk93 Apr 07 '17 at 17:41
1 Answers
4
Your mainactivity:-
package com.example.tts;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;
public class MainActivity extends Activity {
TextToSpeech t1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = "hello world";
Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
public void onPause(){
if(t1 !=null){
t1.stop();
t1.shutdown();
}
super.onPause();
}
}
Shobhit
- 1,086
- 11
- 29
-
-
-
@Subijoy i don't think so there can be any lesser code than this...this is the best way to optimize your texttospeech..!and for the xml part.....that's vey simple......only a button is needed – Shobhit Apr 08 '17 at 06:06
-
-
@Shobhit Would you please guide on query related to this same topic: https://stackoverflow.com/questions/69963428/how-to-implement-error-handling-in-text-to-speech-while-working-with-recycler-vi – SVK Nov 15 '21 at 12:06