当我在android chatbot中输入文本作为输入时,我的文本到语音不起作用

时间:2017-07-15 13:57:50

标签: android text-to-speech speech-to-text chatbot

我使用在线教程制作了一个聊天机器人。现在,除了编辑编辑文本作为输入之外,我还使用语音识别。但问题是,当我按下语音识别按钮时,tts不起作用。我不知道我使用各种方法的问题是什么。从编辑文本字段发送文本时,tts工作正常。这是主要活动中两个代码的示例。第一个代码是通过发送按钮发送文本,工作正常。第二个代码是我用stt聊天而且没有用。需要帮助来解决问题。提前致谢。

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

            }
        });

和使用语音识别的代码,这里的tts不起作用

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

2 个答案:

答案 0 :(得分:0)

只需调用此方法并在此方法中传递文本

 public void textToSpeech(String message){
            if (result==TextToSpeech.LANG_NOT_SUPPORTED ||result==TextToSpeech.LANG_MISSING_DATA){
                Toast.makeText(Activity.this, "Language is Not Supported", Toast.LENGTH_SHORT).show();
            }else {
                String text=message;
                if (text==null||text.equals("")){
                    text="Text is Empty";
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
                } else {
                    textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null);
                }
            }

        }

答案 1 :(得分:0)

好的,我已经解决了问题,而不是ontause在tts之后只使用了onDestry。 并添加了tts.stop();在buSpeak按钮方法中,按下该按钮时停止tts。

`下面的代码

public void buSpeak(查看视图){

    tts.stop();

    startVoiceRecognitionActivity();

}

//after all other steps 

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

}