我創建類似語音識別的準確性遊戲的東西,我使用的是與我需要的名字的話循環,但問題是它只是不斷的結果開始活動後,跑到下一個單詞而沒有給你機會回答。我已經看到有兩種方法可能的替代方案,一種是回調類(從來沒有這樣做過),並以某種方式讓它們相互傳遞,但我不確定它是如何工作的,並且找不到詳細的解釋。這裏是我的方法在for循環Xamarin For循環不會等待onActvitityResult
for (int i = 0; i < vocabWords.Length; i++)
{
WordToGuess.Text = vocabWords[i];
CurrWord = vocabWords[i];
textBox.Text = "";
string messageSpeakNow = "Speak";
var voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt, messageSpeakNow);
voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1500);
voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1500);
voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 15000);
voiceIntent.PutExtra(RecognizerIntent.ExtraMaxResults, 1);
voiceIntent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.Default);
StartActivityForResult(voiceIntent, VOICE);
}
然後,我有我的onactivityresult,這是我希望的循環等待,而是它只是不斷遍地與谷歌麥克風repopping起來,然後停止一次循環完成永不放棄的用戶有機會回答
保護覆蓋無效OnActivityResult(INT requestCode,結果resultVal,意圖數據) {
if (requestCode == VOICE)
{
if (resultVal == Result.Ok)
{
var matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
if (matches.Count != 0)
{
bool saidIt = false;
while (!saidIt)
{
string textInput = textBox.Text + matches[0];
textBox.Text = textInput;
//set alert for executing the task
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Is " + textBox.Text + ", what you said?");
alert.SetPositiveButton("Yes", (senderAlert, args) =>
{
noResponse = false;
saidIt = true;
SetResult(Result.Ok);
//change value write your own set of instructions
//you can also create an event for the same in xamarin
//instead of writing things here
if (textBox.Text == CurrWord)
{
guess(1);
}
else
{
guess(0);
}
});
alert.SetNegativeButton("No", (senderAlert, args) =>
{
//perform your own task for this conditional button click
});
//run the alert in UI thread to display in the screen
RunOnUiThread(() =>
{
alert.Show();
});
}
}
base.OnActivityResult(requestCode, resultVal, data);
}
}
}
看看這一個辦法:http://stackoverflow.com/questions/40614131/android-speech-recognition-pass-data-back-to-xamarin-forms/40616041#40616041 – SushiHangover