【问题标题】:Xamarin.Forms TextToSpeechXamarin.Forms TextToSpeech
【发布时间】:2018-08-04 10:34:50
【问题描述】:

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

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     
    }
  }
}

using System.Threading.Tasks;

namespace LiveScoring {
    public interface ISpeechToText {
        Task<string> SpeechToTextAsync();
    } 
}

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 "";
        }
    }
}

【问题讨论】:

  • 你能在你的界面中显示代码吗?
  • 完成,编辑界面定义。
  • 谢谢,接口看起来不错,所以问题一定出在你的Android项目中实现接口的类。你也可以发布代码吗?
  • 好的,就在这里。

标签: c# android interface xamarin.forms


【解决方案1】:

尝试将其添加到您的namespace LiveScoring.Droid { 行上方,即:

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

这样会注册依赖服务,所以当你使用DependencyService.Get&lt;&gt;()方法时会被调用。

【讨论】:

  • 这是一个很好的猜测,它解决了这个问题,现在它给出了另一个例外:“没有活动来处理意图”,也许这就是因为它说 Forms.Context 已被弃用,我“应该使用本地上下文”?
  • 很可能,我还没有更新我的 Xamarin.Forms 应用程序,所以我仍然在到处使用 Forms.Context.. 我会看看是否能找到获取上下文的正确方法.
  • 没问题,我也找到了这个 Nuget 包,它获取了当前的 Activity:nuget.org/packages/Plugin.CurrentActivity。尝试将其添加到您的 Droid 项目中,看看它是否有效。编辑:实际上这可能更简单:Android.App.Application.Context 而不是 Forms.Context
  • 我试图将 Forms.Context 更改为 ((Activity)Android.App.Application.Context).StartActivityForResult(voiceIntent, VOICE); 但我收到一个异常消息:“无法将 [..hash stuff].MainApplication 转换为 Android.App.Activity”
  • 好的,我刚刚发现这个应该可以解决问题.. davidbritch.com/2017/11/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 2013-11-08
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多