我一直试图在我的安卓应用程序中添加语音到文本,但我一直在使用"Android.Content.ActivityNotFoundAcception“。
我使用的是Android5.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,但是我无法返回结果。
发布于 2019-03-29 08:49:14
如果您获得"Android.Content.ActivityNotFoundAcception".是因为xamarin Dependencyservice只有on活动,而AndroidSpeech不是活动,则不使用StartActivityForResult directly.Solution is获取对当前活动的引用,为此,我们将添加一个名为Plugin.Current activity的nuget。
可以在应用程序类中添加活动回调,因此我的应用程序类如下所示
[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。

https://stackoverflow.com/questions/55404467
复制相似问题