Luca Montecchio
Luca Montecchio

Reputation: 33

Xamarin.Forms TextToSpeech

I'm trying to develop a mobile app with a SpeechToText feature, I found an example here Original Post and i tried to follow its steps, but when i run the application and i tap on the button to record, i get a message saying "Unhandled Exception occurr, No body on method..". I tried to debug and what I get is that its something related to the DependecyService running the SpeechToTextAsync method from the ISpeecehToText interface. Now I dont use interfaces too much so i'm a bit stuck understanding what is causing this error and how to solve it.

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

Upvotes: 3

Views: 209

Answers (1)

sme
sme

Reputation: 4153

Try to add this above your namespace LiveScoring.Droid { line, ie:

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

This way it will register the dependency service, so it will be called when you use the DependencyService.Get<>() method.

Upvotes: 1

Related Questions