Search code examples
androidtext-to-speechspeech-to-textchatbot

My text to speech do not work, when i give speech to text as input in android chatbot


I made a chatbot using online tutorial, Now apart from writing in edit text as input I am using voice recognition also. but the problem is the tts do not work when I am pressing the voice recognition button. I dont know what is the problem I used various methods. tts works fine while sending text from edit text field. here is the sample for two codes in main activity. First code is for sending text via send button and works fine. Second code is the wone I use stt to chat and tts do not work. Need help to fix the problem. Thanks in advance.

public class MainActivity extends AppCompatActivity {

    private ListView mListView;
    private FloatingActionButton mButtonSend;
    private EditText mEditTextMessage;
    private ImageView mImageView;
    public Bot bot;
    public static Chat chat;
    private ChatMessage.ChatMessageAdapter mAdapter;
    public Button buSpeak;
    public TextToSpeech tts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {

                if (status != TextToSpeech.ERROR){
                    tts.setLanguage(Locale.US);

                }

            }
        });

        mListView = (ListView) findViewById(R.id.listView);
        mButtonSend = (FloatingActionButton) findViewById(R.id.btn_send);
        mEditTextMessage = (EditText) findViewById(R.id.et_message);
        mImageView = (ImageView) findViewById(R.id.iv_image);
        mAdapter = new ChatMessage.ChatMessageAdapter(this, new ArrayList<ChatMessage>());
        mListView.setAdapter(mAdapter);
        buSpeak = (Button)findViewById(R.id.buSpeak);

        CheckUserPermsions();

        //chat button

        mButtonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String message = mEditTextMessage.getText().toString();
                //bot
                String response = chat.multisentenceRespond(mEditTextMessage.getText().toString());
                if (TextUtils.isEmpty(message)) {
                    return;
                }
                sendMessage(message);
                mimicOtherMessage(response);
                mEditTextMessage.setText("");
                mListView.setSelection(mAdapter.getCount() - 1);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){

                    tts.speak(response, TextToSpeech.QUEUE_FLUSH,null,null);

                }else{
                    tts.speak(response, TextToSpeech.QUEUE_FLUSH,null);
                }

            }
        });

and the code for Using voice recognition, here the tts do not work

public void buSpeak(View view) {

        startVoiceRecognitionActivity();

    }


    private void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());


        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        //since you only want one, only request 1
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);

        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");

        startActivityForResult(intent, 1234);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == Activity.RESULT_OK){

            tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {

                    if (status != TextToSpeech.ERROR){
                        tts.setLanguage(Locale.US);
                    }
                }
            });

            //pull all of the matches
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            String topResult = matches.get(0);

            EditText AutoText = (EditText) findViewById(R.id.et_message);
            AutoText.setText(topResult);

           String message = AutoText.getText().toString();
            //bot
            String response = chat.multisentenceRespond(AutoText.getText().toString());
            if (TextUtils.isEmpty(response)) {
                return;
            }
            sendMessage(message);
            mimicOtherMessage(response);

            AutoText.setText("");
            mListView.setSelection(mAdapter.getCount() - 1);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){

                tts.speak(response, TextToSpeech.QUEUE_FLUSH,null,null);
            }else{
                tts.speak(response, TextToSpeech.QUEUE_FLUSH,null);
            }
        }
    }

    public void CheckUserPermsions(){
        if ( Build.VERSION.SDK_INT >= 23){
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                    PackageManager.PERMISSION_GRANTED  ){
                requestPermissions(new String[]{
                                android.Manifest.permission.READ_EXTERNAL_STORAGE},
                        REQUEST_CODE_ASK_PERMISSIONS);
                return ;
            }
        }

    }

    //get acces to location permsion
    final private int REQUEST_CODE_ASK_PERMISSIONS = 123;



    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();

                } else {

                    // Permission Denied
                    Toast.makeText( this,"denail" , Toast.LENGTH_SHORT)
                            .show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    public void onPause(){
        if(tts !=null){
            tts.stop();
            tts.shutdown();
        }
        super.onPause();
    }
}

Solution

  • Okay I have solved the problem, Instead of onPause just used onDestry after tts. and added tts.stop(); in buSpeak button method to stop tts when that button is pressed.

    `Code below

    public void buSpeak(View view) {

        tts.stop();
    
        startVoiceRecognitionActivity();
    
    }
    
    //after all other steps 
    
     public void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    
    }