我一直在尝试将语音到文本添加到我的Android应用程序,但我一直得到“Android.Content.ActivityNotFoundAcception”。 我正在使用android 5.0(棒棒糖)模拟器,因为我的电脑无法启动任何其他模拟器。
接口:
public interface ISpeech
{
Task<string> SpeechToTextAsync();
}
创建意图和开始活动:
// 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;
}
}
它一直正常工作,直到获得OnActivityResult,这里抛出异常 主要活动:
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);
}
点击按钮时调用功能:
private async void OnStartBtnClick(object sender, EventArgs args)
{
string speechResults = await DependencyService.Get<ISpeech().SpeechToTextAsync();
Lbl.Text = speechResults;
}
我也尝试过:
[Activity]
public class AndroidSpeech : Activity, ISpeech
{
// this gives Java.Lang.NullPointer Exception
}
我也尝试创建自己的RecognitionListener但我无法返回结果。
如果你得到"Android.Content.ActivityNotFoundAcception".
因为xamarin Dependencyservice只有on活动,并且AndroidSpeech
不是活动,你不直接使用StartActivityForResult
。解决方法是获取对当前活动的引用,为了做到这一点,我们将添加一个nuget叫Plugin.Current activity
您可以在Application类中添加活动回调,因此我的应用程序类看起来像这样
[Application]
public class MainApplication : Application, Application.IActivityLifecycleCallbacks
{
public MainApplication(IntPtr handle, JniHandleOwnership transer)
: base(handle, transer)
{
}
public override void OnCreate()
{
base.OnCreate();
RegisterActivityLifecycleCallbacks(this);
//A great place to initialize Xamarin.Insights and Dependency Services!
}
public override void OnTerminate()
{
base.OnTerminate();
UnregisterActivityLifecycleCallbacks(this);
}
public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
{
CrossCurrentActivity.Current.Activity = activity;
}
public void OnActivityDestroyed(Activity activity)
{
}
public void OnActivityPaused(Activity activity)
{
}
public void OnActivityResumed(Activity activity)
{
CrossCurrentActivity.Current.Activity = activity;
}
public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
{
}
public void OnActivityStarted(Activity activity)
{
CrossCurrentActivity.Current.Activity = activity;
}
public void OnActivityStopped(Activity activity)
{
}
}
在AndroidSpeech类中,您可以获得对此类当前活动的引用
_activity = CrossCurrentActivity.Current.Activity;
然后你可以在StartActivityForResult
中调用AndroidSpeech
有关于如何逐步实现语音到文本的博客,您可以参考它。 https://medium.com/@dev.aritradas/xamarin-forms-speech-recognition-c16f07cdf164
这是演示。 https://github.com/dev-aritra/XFSpeech
更新这是运行GIF。