繁体   English   中英

Xamarin.Forms TextToSpeech

[英]Xamarin.Forms TextToSpeech

我正在尝试开发一个带有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 "";
        }
    }
}

尝试将其添加到namespace LiveScoring.Droid { line,即:

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

这样它将注册依赖服务,因此当您使用DependencyService.Get<>()方法时将调用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM