I've been trying to add speech-to-text to my android app but i'm keep getting "Android.Content.ActivityNotFoundAcception".
I'm using android 5.0 (lollipop) emulator since my computer can't start any other emulator.
Interface:
public interface ISpeech
{
Task<string> SpeechToTextAsync();
}
Creating intent and starting activity:
// When i remove Java.Lang.Object it gives Java.Lang.NullPointer Exception
public class AndroidSpeech : Java.Lang.Object, ISpeech
{
public static AutoResetEvent autoEvent = new AutoResetEvent(false);
private Intent voiceIntent;
public static readonly int speech = 10; // requestCode
public static string speechResults;
public async Task<string> SpeechToTextAsync()
{
var context = Forms.Context;
var activity = (Activity)context;
voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt, "Speak");
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);
autoEvent.Reset();
activity.StartActivityForResult(voiceIntent, speech);
await Task.Run(() => { autoEvent.WaitOne(new TimeSpan(0, 0, 3)); });
return speechResults;
}
}
It works fine until getting OnActivityResult, here it throwes the exception
Main Activity:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == AndroidSpeech.speech)
{
// resultCode returns Result.Canceled
if (resultCode == Result.Ok) // here it throws ActivityNotFoundException
{
var speech = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
AndroidSpeech.speechResults = speech[0];
}
AndroidSpeech.autoEvent.Set();
}
base.OnActivityResult(requestCode, resultCode, data);
}
Calling function on button click:
private async void OnStartBtnClick(object sender, EventArgs args)
{
string speechResults = await DependencyService.Get<ISpeech().SpeechToTextAsync();
Lbl.Text = speechResults;
}
I also tried:
[Activity]
public class AndroidSpeech : Activity, ISpeech
{
// this gives Java.Lang.NullPointer Exception
}
And i also tried creating my own RecognitionListener but i can't return the results.