1
I’m making an app in which I ask a question and the phone answers me as the question asked,but the problem is that the phone listens but does not answer me, IE, does not speak.
public class MainActivity extends AppCompatActivity {
private TextToSpeech myTTS;
private SpeechRecognizer mySpeechReconizer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button mic = (Button) findViewById(R.id.mic);
mic.setOnClickListener((view) ->
{
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
mySpeechReconizer.startListening(i);
});
initializeTextToSpeech();
initializeSpeechReconizer();
}
private void initializeSpeechReconizer() {
if (SpeechRecognizer.isRecognitionAvailable(this)) {
mySpeechReconizer = SpeechRecognizer.createSpeechRecognizer(this);
mySpeechReconizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle bundle) {
List<String> res = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
processResult(res.get(0));
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
}
}
private void processResult(String s) {
s = s.toLowerCase();
//Qual é o teu nome?
if (s.indexOf("What") != -1){
if (s.indexOf("your name") != -1){
speak("two");
}
if (s.indexOf("time") != -1){
Date now = new Date();
String time = DateUtils.formatDateTime(this,now.getTime(),
DateUtils.FORMAT_SHOW_TIME);
speak(time);
}
}
else if (s.indexOf("open") != -1){
if (s.indexOf("browser") != -1){
}
speak("Sou a Lian, fui fundada pelo Leandro e moro em Baião.");
}
}
private void initializeTextToSpeech() {
myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (myTTS.getEngines().size()==0){
Toast.makeText(MainActivity.this, "Teste", Toast.LENGTH_LONG).show();
finish();
} else {
Locale l1 = new Locale("pt","PT");
myTTS.setLanguage(l1);
speak("Olá Leandro, estou pronta,podes falar!");
}
}
});
}
private void speak(String s) {
if (Build.VERSION.SDK_INT >=21){
myTTS.speak(s,TextToSpeech.QUEUE_FLUSH,null,null);
} else{
myTTS.speak(s,TextToSpeech.QUEUE_FLUSH,null);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
myTTS.shutdown();
}
}
Does it speak at any time? For example when starting the textToSpeech?
– Anderson Henrique
Yes speaks what I entered as welcome messages
– leandr7o