Xamarin.Forms TextToSpeech

时间:2018-02-24 13:56:33

标签: c# android interface xamarin.forms

我正在尝试开发一个带有SpeechToText功能的移动应用,我在这里找到了一个示例Original Post,我尝试按照其步骤操作,但是当我运行应用程序并点击按钮时记录,我得到一条消息说"未处理的异常发生,方法没有正文.."。 我尝试调试,我得到的是与DependecyService接口中运行SpeechToTextAsync方法的ISpeecehToText相关的内容。 现在我不太使用接口,所以我有点卡住了解导致此错误的原因以及如何解决它。

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

1 个答案:

答案 0 :(得分:1)

尝试在db.employees.aggregate( [ { $graphLookup: { from: "employees", startWith: "$reportsTo", connectFromField: "reportsTo", connectToField: "name", as: "reportingHierarchy" } }, { $match: { $and: [{ "reportingHierarchy.name": "Andrew" }, {"reportingHierarchy.name": "Eliot" }] } } ] ) 行上方添加此内容,即:

namespace LiveScoring.Droid {

这样它会注册依赖服务,因此当你使用[assembly: Dependency(typeof(SpeechToText))] namespace LiveScoring.Droid { ... } 方法时会调用它。