Home > other >  I want to stop text to speech method when user returning from another activity to MainActivity
I want to stop text to speech method when user returning from another activity to MainActivity

Time:01-10

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?


import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private static final int REQ_CODE_SPEECH_INPUT = 100;
    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);
                        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) {
                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) {

        }
    }

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

}

CodePudding user response:

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

CodePudding user response:

Replace these code in your 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();
}
  •  Tags:  
  • Related