C# Xamarin.Forms TextToSpeech

C# Xamarin.Forms TextToSpeech,c#,android,interface,xamarin.forms,C#,Android,Interface,Xamarin.forms,我正在尝试开发一个带有SpeechToText功能的移动应用程序,我在这里找到了一个示例,并尝试按照它的步骤进行操作,但是当我运行应用程序并点击按钮进行录制时,我收到一条消息说“未处理的异常发生,方法上没有正文…”。 我尝试过调试,得到的结果是它与从ispeechtotext接口运行SpeechToTextAsync方法的dependcyservice有关。 现在我没有太多地使用接口,所以我有点难以理解是什么导致了这个错误以及如何解决它 namespace LiveScoring { pub

我正在尝试开发一个带有SpeechToText功能的移动应用程序,我在这里找到了一个示例,并尝试按照它的步骤进行操作,但是当我运行应用程序并点击按钮进行录制时,我收到一条消息说“未处理的异常发生,方法上没有正文…”。 我尝试过调试,得到的结果是它与从
ispeechtotext
接口运行
SpeechToTextAsync
方法的
dependcyservice
有关。 现在我没有太多地使用接口,所以我有点难以理解是什么导致了这个错误以及如何解决它

namespace LiveScoring {
  public partial class MainPage : ContentPage {
    public MainPage() {
      InitializeComponent();
    }

    public void RecordBtn_Clicked(object sender, EventArgs e) { 
        WaitForSpeechToText();
    }

    private async void WaitForSpeechToText() {
        Output_lbl.Text = await DependencyService.Get<ISpeechToText>().SpeechToTextAsync();
     >> here I get the error     
    }
  }
}
namespace-LiveScoring{
公共部分类主页:ContentPage{
公共主页(){
初始化组件();
}
public void RecordBtn_已单击(对象发送方,事件参数e){
WaitForSpeechToText();
}
私有异步void WaitForSpeechToText()的{
Output_lbl.Text=等待DependencyService.Get().SpeechToTextAsync();
>>这里我得到了错误
}
}
}

使用System.Threading.Tasks;
命名空间活记分{
公共接口ISpeechToText{
任务SpeechToTextAsync();
} 
}

namespace LiveScoring.Droid{
公共类SpeechToText:ISpeechToText{
私人常数int VOICE=10;
公共静态字符串SpeechText;
public static AutoResetEvent autoEvent=新的AutoResetEvent(false);
公共演讲文本(){}
公共异步任务SpeechToTextAsync(){
var tcs=new TaskCompletionSource();
试一试{
var voiceIntent=新意图(RecognizerIntent.ActionRecognizeSpeech);
voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel,RecognizerIntent.LanguageModelFreeForm);
voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt,“立即通话”);
voiceIntent.PutExtra(识别器Intent.ExtraspeechInputCompleteSilenceLength,1500);
voiceIntent.PutExtra(识别器Intent.ExtraspeechInputPossiblyCompleteSilenceLength,1500);
voiceIntent.PutExtra(识别器Intent.ExtraSpeechInputMinimumLengthMillis,15000);
voiceIntent.PutExtra(RecognizerIntent.ExtraMaxResults,1);
PutExtra(RecognizerIntent.ExtraLanguage,Java.Util.Locale.Default);
SpeechText=“”;
autoEvent.Reset();
试一试{
((活动)Forms.Context)StartActivityForResult(语音意图,语音);
}捕获(ActivityNotFoundException a){
SetResult(“设备不支持语音到文本”);
}
wait Task.Run(()=>{autoEvent.WaitOne(新的时间跨度(0,2,0));});
返回演讲文本;
}捕获(例外情况除外){
tcs.SetException(ex);
}     
返回“”;
}
}
}

尝试将此添加到
名称空间LiveScoring.Droid{
行上方,即:

[assembly: Dependency(typeof(SpeechToText))]
namespace LiveScoring.Droid {
...
}

这样,它将注册依赖项服务,因此在使用
DependencyService.Get()时将调用它
method.

你能在界面中显示代码吗?完成,用界面定义编辑。谢谢,界面看起来很好,所以问题一定是在Android项目中实现该界面的类中。你也能发布该类代码吗?当然,在这里。这是一个很好的猜测,它解决了该问题,现在它给出了另一个例外:“没有处理意图的活动”,这可能是因为它说
Forms.Context
不受欢迎,我“应该使用本地上下文”"?很可能,我还没有更新我的Xamarin.Forms应用程序,所以我仍然在使用Forms.Context everywhere..我会看看是否能找到获取上下文的正确方法。没问题,我还找到了这个Nuget包,它获取当前活动:。尝试将它添加到Droid项目中,看看它是否有效。编辑:实际上这可能更简单:
 Android.App.Application.Context
而不是
Forms.Context
我试图将Forms.Context更改为
((活动)Android.App.Application.Context)。StartActivityForResult(voiceIntent,VOICE);
但我遇到一个异常,说:“无法将[…散列内容].MainApplication转换为Android.App.Activity”好的,我刚找到这个应该可以解决问题的。。
namespace LiveScoring.Droid {
    public class SpeechToText : ISpeechToText {
        private const int VOICE = 10;
        public static string SpeechText;
        public static AutoResetEvent autoEvent = new AutoResetEvent(false);

        public SpeechToText() { }

        public async 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, "Talk now");
                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);

                SpeechText = "";
                autoEvent.Reset();


                try {
                    ((Activity)Forms.Context).StartActivityForResult(voiceIntent, VOICE);
                } catch (ActivityNotFoundException a) {
                    tcs.SetResult("Device doesn't support speech to text");
                }

                await Task.Run(() => { autoEvent.WaitOne(new TimeSpan(0, 2, 0)); });
                return SpeechText;

            } catch (Exception ex) {
                tcs.SetException(ex);
            }     

            return "";
        }
    }
}
[assembly: Dependency(typeof(SpeechToText))]
namespace LiveScoring.Droid {
...
}