Search code examples
c#xamarinpermissionsrequestspeech

In C#, I am getting an error when requesting permission for speech recognition Xamarin Android


So I am using the Plugin.SpeechRecognition Nuget Package and following the exact code on line and its not working.

I have tried adding the "Plugin.Permissions" Nuget Package and that hasn't helped and i have tried googling the problem but there isn't anyone getting this issue and it seems to work fine for everyone. I have also tried removing the "await" keyword and it just says

Operator '==' cannot be applied to operands of type 'IObservable' and 'bool'

Here is my code:

    private async void GetSpeechPermission()
    {
        var granted = await CrossSpeechRecognition.Current.RequestPermission();
        if (granted == true)
        {
            // go!
        }
    }

so what should happen is there is no error what so ever and the code should run fine but the line of code

await CrossSpeechRecognition.Current.RequestPermission();

has a red underline saying

IObservable' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IObservable' could be found (are you missing a using directive or an assembly reference?)

when I am using the EXACT code provided by the creator of the plugin from here https://github.com/aritchie/speechrecognition

Any help is MUCH appreciated!!


Solution

  • The Solution to this was to add

    using System.Reactive.Linq

    in the using section of the code and instead of using a bool value as the code example for the plugin suggests, instead, in the if statement, convert the "granted" variable to a string and then check for "Available", Code:

        private async void GetSpeechPermission()
        {
            var granted = await CrossSpeechRecognition.Current.RequestPermission();
            if (granted.ToString() == "Available")
            {
                //GO
            }
        }
    

    Hope this helps some one! :D