question

BrianErickson-9266 avatar image
0 Votes"
BrianErickson-9266 asked ·

How to wait for speech recognition callback

I've got the Android speech recognition stuff working well but have a question.

I've got a method called StartListening(). It's starts the speech recognition mechanism and when the OnResult call back gets fired the caller is notified via an event notification.

Now, what I'd like is a method call Listen. It should return a string consisting of the words spoken by the user. The best I've been able to come with is:

       async public Task<string> Listen()
       {
          Words = null;
          GlobalVariables.SpokenWords.SpokenWordsEvent += SpokenWordsEventHandler;
          Device.BeginInvokeOnMainThread(() =>
          {
             StartListening();
          });
    
          while (Words == null)
             await Task.Delay(100);
    
          GlobalVariables.SpokenWords.SpokenWordsEvent -= SpokenWordsEventHandler;
    
          return (Words);
       }

It does work but is very ugly. I'm sure there's a better way but I don't see it...any help? It would be ideal if it was not marked async...

dotnet-android
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered ·

Hello,

Welcome to Microsoft Q&A!

Take a look at TaskCompletionSource , It enables the creation of a task that can be handed out to consumers.

Sample


public Task<string> MyTask()
{

    var tcs = new TaskCompletionSource<string>();

    {
        //OnResult call back
        tcs.SetResult(Words);
    }

    return tcs.Task;

}


BTW , I would suggest you use official sample for SpeechToText.

We could handle the callback in OnActivityResult method in activity.



If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 5 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

There's something I'm not understanding... This just hangs as result is never set because listening never starts.

  async public Task<string> Listen()
           {
              string Words;
              GlobalVariables.SpokenWords.SpokenWordsEvent += SpokenWordsEventHandler;
              ListenResult = new TaskCompletionSource<string>();
        
              Device.BeginInvokeOnMainThread(() =>
              {
                StartListening();
              });
        
              Words = ListenResult.Task.Result;
        
              //while (Words == null)
              //   await Task.Delay(100);
        
              ListenResult = null;
              GlobalVariables.SpokenWords.SpokenWordsEvent -= SpokenWordsEventHandler;
        
              return (Words);
           }
0 Votes 0 ·

FWIW, the speech to text is performed in a foreground service and is loosely based on the sample. I used event notifications as that mechanism works for UWP as well.

0 Votes 0 ·
ColeXia-MSFT avatar image ColeXia-MSFT BrianErickson-9266 ·

Could you post the code in SpokenWordsEventHandler ?

0 Votes 0 ·

Here it is...
private void SpokenWordsEventHandler(object sender, SpokenWordsEventArgs e)
{
if (e.Error == 0)
{
if (ListenResult != null)
ListenResult.SetResult(e.Words);
Words = e.Words;
}
else
{
Device.BeginInvokeOnMainThread(() =>
{
StartListening();
});
}
}

0 Votes 0 ·

This appears to be helpful when getting results from a task in a different thread. Just hangs if things are in the same thread. I'll stick with my delay loop.

0 Votes 0 ·