I want to stop text to speech method when user returning from another activity to MainActivity
Basically i have two java activities where i have implemented textToSpeech in one activity but when i go to another activity using on click view and when i pressed back for returning to the mainactivity then it will again execute texttospeech method. But i want to execute this method only once when the user starts the app after that it will stop. Below is my code and i want to execute the code only once how it will done?
package com.example.software2.ocrhy;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final int REQ_CODE_SPEECH_INPUT = 100;
private static int firstTime = 0;
private TextView mVoiceInputTv;
float x1, x2, y1, y2;
private TextView mSpeakBtn;
private static TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.US);
textToSpeech.setSpeechRate(1.1f);
if (firstTime == 0)
textToSpeech.setSpeechRate(1.1f);
textToSpeech.speak("Welcome to Blind App. Swipe left to listen the features of the app or swipe right and say what you want", TextToSpeech.QUEUE_FLUSH, null);
}
}
});
mVoiceInputTv = (TextView) findViewById(R.id.voiceInput);
}
public boolean onTouchEvent(MotionEvent touchEvent) {
firstTime = 1;
switch (touchEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = touchEvent.getX();
y1 = touchEvent.getY();
break;
case MotionEvent.ACTION_UP:
x2 = touchEvent.getX();
y2 = touchEvent.getY();
if (x1 < x2) {
textToSpeech.speak(" Say Read for reading, calculator for calculator, time and date, weather for weather, battery for battery. Swipe right and say what you want ", TextToSpeech.QUEUE_FLUSH, null);
} else if (x1 > x2) {
startVoiceInput();
}
break;
}
return false;
}
private void startVoiceInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello, How can I help you?");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
a.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE_SPEECH_INPUT) {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
mVoiceInputTv.setText(result.get(0));
if (mVoiceInputTv.getText().toString().equals("read")) {
Intent intent = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(intent);
mVoiceInputTv.setText(null);
} else {
textToSpeech.speak("Do not understand just tap on the screen Say again", TextToSpeech.QUEUE_FLUSH, null);
}
if (mVoiceInputTv.getText().toString().equals("calculator")) {
Intent intent = new Intent(getApplicationContext(), MainActivity3.class);
startActivity(intent);
mVoiceInputTv.setText(null);
} else {
textToSpeech.speak("Do not understand just tap on the screen Say again", TextToSpeech.QUEUE_FLUSH, null);
}
if (mVoiceInputTv.getText().toString().equals("time and date")) {
Intent intent = new Intent(getApplicationContext(), MainActivity4.class);
startActivity(intent);
mVoiceInputTv.setText(null);
} else {
textToSpeech.speak("Do not understand just tap on the screen Say again", TextToSpeech.QUEUE_FLUSH, null);
}
if (mVoiceInputTv.getText().toString().equals("weather")) {
Intent intent = new Intent(getApplicationContext(), MainActivity5.class);
startActivity(intent);
mVoiceInputTv.setText(null);
} else {
textToSpeech.speak("Do not understand just tap on the screen Say again", TextToSpeech.QUEUE_FLUSH, null);
}
if (mVoiceInputTv.getText().toString().equals("battery")) {
Intent intent = new Intent(getApplicationContext(), MainActivity6.class);
startActivity(intent);
mVoiceInputTv.setText(null);
} else {
textToSpeech.speak("Do not understand tap on the screen Say again", TextToSpeech.QUEUE_FLUSH, null);
}
if (mVoiceInputTv.getText().toString().equals("yes")) {
textToSpeech.speak(" Say Read for reading, calculator for calculator, time and date, weather for weather, battery for battery. Do you want to listen again", TextToSpeech.QUEUE_FLUSH, null);
mVoiceInputTv.setText(null);
} else if ((mVoiceInputTv.getText().toString().equals("no"))) {
textToSpeech.speak("then tap on the screen and say what you want", TextToSpeech.QUEUE_FLUSH, null);
}
} else if (mVoiceInputTv.getText().toString().equals("exit")) {
finish();
}
}
}
public void onPause() {
if (textToSpeech != null) {
textToSpeech.stop();
}
super.onPause();
}
public boolean onKeyDown(int keyCode, @Nullable KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
textToSpeech.speak("You are already in the main menu", TextToSpeech.QUEUE_FLUSH, null);
}
return true;
}
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
}
Make Text to Speak variable in Global and at these code in your onStop / onPause method.
public static void releaseTts() {
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
}