当用户从另一个活动返回到MainActivity时,我想停止文本到语音的方法



基本上,我有两个java活动,其中我在一个活动中实现了textToSpeech,但当我使用点击视图转到另一个活动时,当我按下返回主活动时,它将再次执行textToSpeechmethod。但是我只想在用户启动应用程序时执行一次这个方法,之后它就会停止下面是我的代码,我只想执行一次代码,它将如何完成?

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);
}
}

在Global中以及在onStop/onPause方法中的这些代码中生成Text to Speak变量。

public static void releaseTts() {
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
}

MainActivity.class中替换这些代码

private static final int REQ_CODE_SPEECH_INPUT = 100;
private static  int firstTime = 0;
private TextView mVoiceInputTv;
private ImageButton 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);
if (firstTime == 0)
textToSpeech.speak("Welcome to Blind App. Tap on the screen. Say Read for reading . Say  calculator  for calculator.say time and date. say weather for weather. say battery for battery.", TextToSpeech.QUEUE_FLUSH, null);
}
}
});
mVoiceInputTv = (TextView) findViewById(R.id.voiceInput);
mSpeakBtn = (ImageButton) findViewById(R.id.btnSpeak);
mSpeakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstTime = 1;
startVoiceInput();
}
});
}
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);
}
if (mVoiceInputTv.getText().toString().equals("calculator")) {
Intent intent = new Intent(getApplicationContext(), MainActivity3.class);
startActivity(intent);
mVoiceInputTv.setText(null);
}
if (mVoiceInputTv.getText().toString().equals("time and date")) {
Intent intent = new Intent(getApplicationContext(), MainActivity4.class);
startActivity(intent);
mVoiceInputTv.setText(null);
}
if (mVoiceInputTv.getText().toString().equals("weather")) {
Intent intent = new Intent(getApplicationContext(), MainActivity5.class);
startActivity(intent);
mVoiceInputTv.setText(null);
}
if (mVoiceInputTv.getText().toString().equals("battery")) {
Intent intent = new Intent(getApplicationContext(), MainActivity6.class);
startActivity(intent);
mVoiceInputTv.setText(null);
}
}
}
public void onPause() {
if (textToSpeech != null) {
textToSpeech.stop();
}
super.onPause();
}

最新更新