【问题标题】:Cancel SpeechRecognition if nothing captured如果未捕获任何内容,则取消 SpeechRecognition
【发布时间】:2018-11-22 00:39:37
【问题描述】:

我正在尝试开发一款能够倾听并回馈用户的应用。我正在努力让它尽可能免提。

我的问题是,如果用户没有及时响应,SpeechRecognition 会超时,用户需要按下按钮重新开始收听。

*我有没有办法解决如果应用程序没有听到任何声音,它会提示重试并重新启动侦听器?

代码:

//Function i call when a user input is required.
private void promptSpeechInput() {
    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());
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

/**
 * Receiving speech input
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    System.out.println("REQUEST CODE: " + requestCode);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            System.out.println("resultCode: " + resultCode);
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                txtSpeechInput.setText(result.get(0));
                input = result.get(0).toLowerCase();
            }
            break;
        }

    }
}

我还有代码可以向用户读取文本,然后在完成后提示语音输入。

如果我可以提供更多详细信息或代码,请告诉我。非常感谢!

【问题讨论】:

    标签: java android speech-recognition


    【解决方案1】:

    要在不点击“麦克风按钮”的情况下让谷歌语音免提,你必须为识别器创建自己的类,我在 xamarin-android 中编写了代码,所以它在 java 上非常相似:

    Public class CustomRecognizer : Java.Lang.Object, IRecognitionListener, TextToSpeech.IOnInitListener
    {
    private SpeechRecognizer _speech;
    private Intent _speechIntent;
    
    
    public string Words;
    
    
    public CustomRecognizer(Context _context)
    {
        this._context = _context;
        Words = "";
        _speech = SpeechRecognizer.CreateSpeechRecognizer(this._context);
        _speech.SetRecognitionListener(this);
        _speechIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
        _speechIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
        _speechIntent.PutExtra(RecognizerIntent.ActionRecognizeSpeech, RecognizerIntent.ExtraPreferOffline);
        _speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1000); 
        _speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1000);
        _speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 1500);
    }
    
    void startover()
    {
        _speech.Destroy();
        _speech = SpeechRecognizer.CreateSpeechRecognizer(this._context);
        _speech.SetRecognitionListener(this);
        _speechIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
        _speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1000);
        _speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1000);
        _speechIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 1500);
    StartListening();
    }
    public void StartListening()
    {
        _speech.StartListening(_speechIntent);
    }
    
    public void StopListening()
    {
        _speech.StopListening();
    }
    
    public void OnBeginningOfSpeech()
    {
    
    }
    
    public void OnBufferReceived(byte[] buffer)
    {
    }
    
    public void OnEndOfSpeech()
    {
    
    }
    
    public void OnError([GeneratedEnum] SpeechRecognizerError error)
    {
        Words = error.ToString();
        startover();
    }
    

    当识别器超时时,会调用 OnError 事件 .在我的代码中,我使用 startover() 来重新开始录制。

    【讨论】:

    • 这个答案对我有用,只需稍作改动。我很高兴能够在我以前使用 Servcie 作为侦听器的情况下使用这种方法。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-03-18
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多