Android - 语音识别器语音意图错误处理(C#)

时间:2017-09-05 15:57:27

标签: c# android speech-recognition voice-recognition recognizer-intent

public class createRecognizerIntent
{
    var voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);

    voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
    voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt,
    Application.Context.GetString(Resource.String.ApplicationName));
    voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1500);
    voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1500);
    voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 15000);
    voiceIntent.PutExtra(RecognizerIntent.ExtraMaxResults, 1);
    voiceIntent.PutExtra(RecognizerIntent.ExtraLanguage, "de");
    voiceIntent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.Default);
    StartActivityForResult(voiceIntent, VOICE);}

我有这个代码来创建识别器意图。现在我想在出现错误时重新启动intent(“createRecognizerIntent”方法)。我怎么能意识到这一点?

1 个答案:

答案 0 :(得分:0)

要重新启动intent,您必须创建自己的识别器,这是我在xamarin上的代码:

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

当出现错误时( OnError()),您可以调用 startover()