1

I would like to iterate through all voices that are installed on a device.

In the TextSoSpeech metadata I see that there's

namespace Android.Speech.Tts
{
    public class TextToSpeech : Java.Lang.Object
    {
        [Obsolete("deprecated")]
        public virtual ICollection<Voice> Voices { get; }

Even though it's obsolete, I would like to use "public virtual ICollection Voices { get; }".

I don't know any other way how to get the installed voices with Xamarin.

However, I've never iterated through an ICollection.

How would that be done?

I tried starting with

ICollection<Voice>nVoices = Android.Speech.Tts.TextToSpeech.

But ".Voices" is not a part of that namespace.

tw2017
  • 105
  • 1
  • 6

1 Answers1

1

Voices is not a static property which is why you need an instance of the TextToSpeech class to iterate it. To obtain one though, you'll need to implement the IOnInitListener interface:

public class Speaker : Java.Lang.Object, TextToSpeech.IOnInitListener
{
    private readonly TextToSpeech speaker;

    public Speaker(Context context)
    {
        speaker = new TextToSpeech(context, this);
        // Don't use speaker.Voices here because it hasn't
        // been initialized. Wait for OnInit to be called.
    }

    public void OnInit(OperationResult status)
    {
        if (status.Equals(OperationResult.Success))
        {
            // Iterating the collection with a foreach
            // is perfectly fine.
            foreach (var voice in speaker.Voices)
            {
                // Do whatever with the voice
            }
        }
    }
}

Then from your activity, you can use it like:

var speaker = new Speaker(this);
Nikola Irinchev
  • 1,911
  • 1
  • 13
  • 17