Azure Cognitive Speech Service returning "No Match" always in MAUI application

525 Views Asked by At

I am trying to build simple speech to text android application using .Net MAUI but always getting result as - Microsoft.CognitiveServices.Speech.ResultReason.NoMatch.

Same code if I tried using console application and it is working as expected and is returning the spoken text in the result.

Below is the code which I am using in MAUI -

async void TranscribeClicked(object sender, EventArgs e) {
  bool isMicEnabled = await micService.GetPermissionAsync();
  var audioConfig = AudioConfig.FromDefaultMicrophoneInput();

  if (!isMicEnabled) {
    UpdateTranscription("Please grant access to the microphone!");
    return;
  }

  var config = SpeechConfig.FromSubscription("SubscriptionKey", "ServiceRegion");
  config.SpeechSynthesisVoiceName = "en-US-AriaNeural";

  using(var recognizer = new SpeechRecognizer(config, autoDetectSourceLanguageConfig)) {

    var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

    if (result.Reason == ResultReason.RecognizedSpeech) {
      var lidResult = AutoDetectSourceLanguageResult.FromResult(result);
      UpdateTranscription(result.Text);
    } else if (result.Reason == ResultReason.NoMatch) {
      UpdateTranscription("NOMATCH: Speech could not be recognized.");
    }

  }
}

Not sure what is missing as same code works in Console Application.

1

There are 1 best solutions below

22
On

I tried the below .Net MAUI code and got the text output with input speech.

Code:

MainPage.xaml.cs:

using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using System;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Microsoft.Maui.Controls;

namespace SpeechToTextMauiApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void TranscribeClicked(object sender, EventArgs e)
        {
            bool isMicrophoneEnabled = await CheckMicrophonePermissionAsync();
            if (!isMicrophoneEnabled)
            {
                await DisplayAlert("Need Permission", "Grant access to microphone", "OK");
                return;
            }

            var audioConfig = AudioConfig.FromDefaultMicrophoneInput();
            string subscriptionKey = "<key>";
            string serviceRegion = "<region>";

            var config = SpeechConfig.FromSubscription(subscriptionKey, serviceRegion);
            config.SpeechSynthesisVoiceName = "en-US-AriaNeural";

            using (var recognizer = new SpeechRecognizer(config, audioConfig))
            {
                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    await DisplayAlert("Transcription", $"Recognized Text: {result.Text}", "OK");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    await DisplayAlert("Transcription", "NOMATCH: Speech could not be recognized.", "OK");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    await DisplayAlert("Transcription", $"CancellationReason: {cancellation.Reason}", "OK");
                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        await DisplayAlert("Transcription", $"ErrorDetails: {cancellation.ErrorDetails}", "OK");
                    }
                }
            }
        }

        private Task<bool> CheckMicrophonePermissionAsync()
        {
            return Task.FromResult(true);
        }
    }
}

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:SpeechToTextMauiApp"
             x:Class="SpeechToTextMauiApp.MainPage">

    <StackLayout>
        <Label Text="Speech-to-Text Conversion - MAUI App"
               HorizontalOptions="CenterAndExpand"
               VerticalOptions="CenterAndExpand" />

        <Button Text="Start Transcription" Clicked="TranscribeClicked" />
    </StackLayout>

</ContentPage>

Output:

It runs successfully as below,

enter image description here

Then, I click on Start Transcription to get the text output with the input speech,

enter image description here

I spoke a line and it gives Recognized Text output as below,

enter image description here