如何在Google Recognizer Intent(语音识别)Android

时间:2018-04-17 13:41:13

标签: android android-intent speech-recognition

我确实尝试以毫秒为单位给出以下给出的附加内容

Recognizer Intent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
Recognizer Intent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
Recognizer Intent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS

但不影响语音听时间! 我现在的语音听时间只有3秒! 如何实现10秒的聆听时间

1 个答案:

答案 0 :(得分:0)

它似乎只适用于以下ICS: https://stackoverflow.com/a/17675098/9427932

但是您可以通过创建自己的类来自定义Google语音并继承语音识别器,我在Xamarin Android上编写了这些代码,因此它与android非常相似:

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

public void OnEvent(int eventType, Bundle @params)
{
}

public void OnPartialResults(Bundle partialResults)
{
}

public void OnReadyForSpeech(Bundle @params)
{
}

public void OnResults(Bundle results)
{

    var matches = results.GetStringArrayList(SpeechRecognizer.ResultsRecognition);
    if (matches == null)
        Words = "Null";
    else
        if (matches.Count != 0)
        Words = matches[0];
    else
        Words = "";

    //do anything you want for the result
    }
    startover();
}

public void OnRmsChanged(float rmsdB)
{

}

public void OnInit([GeneratedEnum] OperationResult status)
{
    if (status == OperationResult.Error)
        txtspeech.SetLanguage(Java.Util.Locale.Default);
}}

你可以打电话给 startover(),在结束后立即开始录音,这样看起来像是一个“连续的演讲”(在你的情况下是10秒)