简体   繁体   English

Android TextToSpeech.speak在onActivityResult中不起作用

[英]Android TextToSpeech.speak not working in onActivityResult

I've got a speakText() method as follows 我有一个speakText()方法如下

public void speakText(){
        String toSpeak = "Testing 1 2 3";
        Toast.makeText(getApplicationContext(), toSpeak, 
        Toast.LENGTH_SHORT).show();
        mTts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}

It works fine with a button push (ie calling this method from an onClick function). 按下按钮即可正常工作(即从onClick函数调用此方法)。 However when I call this method inside onActivityResult() as shown below, I can't hear a thing 但是,当我在onActivityResult()中调用此方法时,如下所示,我听不到任何声音

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_running_ui);
    ...
    speakButton = (Button) findViewById(R.id.speakButton);
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if(activities.size()==0){
        speakButton.setEnabled(false);
        Toast.makeText(getApplicationContext(), "Speech Recognizer Not Found", 1000).show();
    }
    speakButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            startVoiceRecognitionActivity();
        }
    });

    commandText = (TextView) findViewById(R.id.command);
}

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

    case MY_DATA_CHECK_CODE:
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            mTts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {

                @Override
                public void onInit(int arg0) {
                    // TODO Auto-generated method stub
                    mTts.setLanguage(Locale.UK);

                }
            });


        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    case VOICE_RECOGNITION_REQUEST_CODE:
        if (resultCode == RESULT_OK){
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            commandText.setText(matches.get(0)); // get the first matching text

            speakText();

        }
    }
}

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

What I'm trying to do is to let the android phone speak something after a speech to text conversion. 我正在尝试做的是让Android手机在语音转换为文本后说出一些话。 I'm sure the program went in VOIC_RECOGNITION_REQUEST_CODE branch as it displayed the text with the commandText.setText(matches.get(0)); 我确定程序进入VOIC_RECOGNITION_REQUEST_CODE分支,因为它使用commandText.setText(matches.get(0))显示文本。 line. 线。

I've got no clue why the textToSpeech works with a button press but not inside the onActivityResult method. 我不知道为什么textToSpeech可以通过按下按钮来工作,而不能在onActivityResult方法中使用。

You shutdown the TextToSpeech in onPause, thus mTts is not binded to the Text to speech engine anymore. 您在onPause中关闭了TextToSpeech,因此mTts不再绑定到文本到语音引擎。 You need to move your code in onPause() to onStop() if what you do is to only to show the recognizer dialog. 如果要做的只是显示识别器对话框,则需要将onPause()中的代码移动到onStop()中。

When the speech recognizer dialog is shown, your activity onPause() is called, but not onStop() unless your activity is not visible anymore. 显示语音识别器对话框时,将调用您的活动onPause(),但不会调用onStop(),除非您的活动不再可见。 You should also instanstiate the text to speech again in onStart(). 您还应该在onStart()中再次实例化语音文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM