Home > Back-end >  How to play text to speech using xamarin.essential
How to play text to speech using xamarin.essential

Time:11-29

I want to play text to speech but I can't hear anything using Xamarin.essential. I 'm sure I don't have muted smartphone. The code:

    using Xamarin.Essential


   private void PlayTextMethod()
   {
     if(do sth)
     {
      SpeekFrommethodAsync();
     }

   }

    
    private async SpeekFrommethodAsync()
    {
        await TextToSpeech.SpeakAsync(Tasklabel.Text, new SpeechOptions
        {
            Volume = 1f
        });
    }
      

CodePudding user response:

If your project's Target Android version is set to Android 11 (R API 30), Open the AndroidManifest.xml file under the Properties folder and add the following inside of the manifest node:

<queries>
  <intent>
    <action android:name="android.intent.action.TTS_SERVICE" />
  </intent>
</queries>

And try this in your method:

 public async Task SpeakNow()
 {
   var locales = await TextToSpeech.GetLocalesAsync();

   // Grab the first locale
   var locale = locales.FirstOrDefault();

   var settings = new SpeechOptions()
    {
        Volume = .75f,
        Pitch = 1.0f,
        Locale = locale
    };

   await TextToSpeech.SpeakAsync("Hello World", settings);
 }

Refer: enter image description here

  • Related