0

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 Answers1

0

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

Dasari Kamali
  • 811
  • 2
  • 2
  • 6
  • Hello Kamali thanks for your response. I tried your code and now I'm getting below error - Reason:Error ErrorDetails:<0x15 (SPXERR_MIC_ERROR) SessionId: 33c336d3e1da487db7461613f7e62ac0> – Mithilesh Jul 23 '23 at 13:28
  • @Mithilesh The error you encountered, "Reason: Error ErrorDetails:<0x15 (SPXERR_MIC_ERROR) SessionId: 33c336d3e1da487db7461613f7e62ac0>", indicates that there was an issue with the microphone during the speech recognition process. This error code, SPXERR_MIC_ERROR, typically occurs when there are problems with accessing or using the microphone input. – Dasari Kamali Jul 23 '23 at 15:40
  • @Mithilesh Make sure that your application has the necessary permissions to access the device's microphone. By checking microphone permissions, ensuring microphone availability, and handling errors more effectively, you should be able to troubleshoot and resolve the "SPXERR_MIC_ERROR" issue. If the problem persists, double-check your subscription key and service region to ensure they are correct and valid for the Microsoft Cognitive Services Speech API. – Dasari Kamali Jul 23 '23 at 15:41
  • @Mithilesh Can you able to solve the error, do you get the text output? – Dasari Kamali Jul 28 '23 at 03:52
  • @Kamali I tried the same code on different machine but the result is same I am not able to get text output. I also checked with mic settings and I think they are fine because the code in console app works fine as I am getting text output. I am still checking for the solution. – Mithilesh Jul 28 '23 at 09:28
  • @Mithilesh Same error you are getting? – Dasari Kamali Jul 28 '23 at 09:31
  • @Mithilesh Can you try to create another Azure **Speech service** in the **Azure portal**. Use that **key** and **region** in the code once. – Dasari Kamali Jul 28 '23 at 09:38
  • 1
    @Kamali I can try that and update you – Mithilesh Jul 28 '23 at 09:58
  • @Kamali I created new service as suggested but result is same getting "SPXERR_MIC_ERROR" error. I created new MAUI application as well using .Net 7 Framework. Installed 1.30 Microsoft.CognitiveServices.Speech nuget and using Using API 33 (Android 13) Emulators – Mithilesh Jul 31 '23 at 07:54
  • @Mithilesh Ok, I will guide you on how to do this in visual studio 2022, follow the same in the below comments. – Dasari Kamali Jul 31 '23 at 08:04
  • @Mithilesh Open visual studio 2022, search for .Net MAUI, and follow the below steps, [step-1](https://i.imgur.com/aGGmMs7.png), [step-2](https://i.imgur.com/yCeekmR.png), [step-3](https://i.imgur.com/qy7EhE6.png) upto this will create a sample project on .Net MAUI App. – Dasari Kamali Jul 31 '23 at 08:37
  • @Mithilesh Then you will get a sample .Net MAUI App code project. – Dasari Kamali Jul 31 '23 at 08:37
  • @Mithilesh You need to add the **above code** I provided in **MainPage.xaml.cs**, **MainPage.xaml** and edit [AppShell.xml](https://i.imgur.com/wpWINCq.png) . – Dasari Kamali Jul 31 '23 at 08:54
  • @Mithilesh Below are the packages you need to install [package-1](https://i.imgur.com/OaZ17ik.png), [package-2](https://i.imgur.com/yF2dVkh.png) and replace your **key** and **region** of Azure speech-services from **Azure portal** and run the code like [this](https://i.imgur.com/8f7f8MA.png). – Dasari Kamali Jul 31 '23 at 09:00
  • @Mithilesh Follow all the steps I gave above and confirm whether you got the output. – Dasari Kamali Jul 31 '23 at 09:02
  • @Kamali I followed all the steps and now getting below error message : System.DllNotFoundException: libMicrosoft.CognitiveServices.Speech.core.so – Mithilesh Jul 31 '23 at 09:41
  • @Mithilesh same **Microsoft.CognitiveServices.Speech = 1.23.0** package you are using? – Dasari Kamali Jul 31 '23 at 09:45
  • @Mithilesh Are you trying this in **Windows?** – Dasari Kamali Jul 31 '23 at 09:46
  • @Kamali Yes I am using Microsoft.CognitiveServices.Speech version 1.23.0 and trying on Windows machine – Mithilesh Jul 31 '23 at 09:55
  • @Mithilesh Ok, Can you try to install another **version** of **Microsoft.CognitiveServices.Speech** from **Managage NuGet Packages** in **Visual Studio 2022** and try to **run** the code again. Thanks. – Dasari Kamali Jul 31 '23 at 10:02
  • @Kamali I tried with different versions of Microsoft.CognitiveServices.Speech. Versions above 1.23.0 is giving me "SPXERR_MIC_ERROR" and version 1.23.0 below is giving me System.DllNotFoundException: libMicrosoft.CognitiveServices.Speech.core.so error. – Mithilesh Jul 31 '23 at 17:13
  • @Mithilesh please check this sample https://github.com/Azure-Samples/cognitive-services-speech-sdk/blob/master/samples/csharp/maui/speech-to-text/speech-to-text/MainPage.xaml.cs and see how microphone access is requested there. Also please update your SDK to latest 1.31.0 – jhakulin Aug 21 '23 at 21:27