im really stuck right now and im quite new to Xamarin. I use Xamarin Forms to develop an App with speech recognition.
I created just a simple UI with a button and an entry-box.
Working:
- Press button and show popup with speech recognition
- read the spoken words into a var
Not Working:
- pass the data back to the Xamarin Forms UI (entry)
StartPage.xaml.cs:
private void BtnRecord_OnClicked(object sender, EventArgs e)
{
WaitForSpeechToText();
}
private async void WaitForSpeechToText()
{
EntrySpeech.Text = await DependencyService.Get<Listener.ISpeechToText>().SpeechToTextAsync();
}
ISpeechToText.cs:
public interface ISpeechToText
{
Task<string> SpeechToTextAsync();
}
to call native code.
SpeechToText_Android.cs:
public class SpeechToText_Android : ISpeechToText
{
private const int VOICE = 10;
public SpeechToText_Android() { }
public Task<string> SpeechToTextAsync()
{
var tcs = new TaskCompletionSource<string>();
try
{
var voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt, "Sprechen Sie jetzt");
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);
try
{
((Activity)Forms.Context).StartActivityForResult(voiceIntent, VOICE);
}
catch (ActivityNotFoundException a)
{
tcs.SetResult("Device doesn't support speech to text");
}
}
catch (Exception ex)
{
tcs.SetException(ex);
}
return tcs.Task;
}
}
MainActivity.cs:
protected override void OnActivityResult(int requestCode, Result resultVal, Intent data)
{
if (requestCode == VOICE)
{
if (resultVal == Result.Ok)
{
var matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
if (matches.Count != 0)
{
string textInput = matches[0].ToString();
if (textInput.Length > 500)
textInput = textInput.Substring(0, 500);
}
// RETURN
}
}
base.OnActivityResult(requestCode, resultVal, data);
}
First i thought that i can pass the result with
return tcs.Task;
back to the ui, but then i noticed that this return is happening when the pop up of the speech recognition has finished to render. At this moment there was not a single word spoken.
The spoken words are in the string "textInput" in the OnActivityResult function, but how can i pass this string back to the Xamarin.Forms UI?
Thanks guys !
See Question&Answers more detail:os