What should i assign the variable listen to ? TextToSpeech.IOnInitListener listen;

This is my code:

`using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Speech.Tts;
using System.IO;
using System.Windows;
using System.Collections.Generic;
using System.Linq;

namespace App5
{
[Activity(Label = "App5", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
TextToSpeech.IOnInitListener listen;

    public MainActivity()
    {

        var langAvailable = new List<string> { "Default" };
        var localesAvailable = Java.Util.Locale.GetAvailableLocales().ToList();
        foreach (var locale in localesAvailable)
        {
            var tts = new TextToSpeech(ApplicationContext,listen);
            LanguageAvailableResult res = tts.IsLanguageAvailable(locale);
            switch (res)
            {
                case LanguageAvailableResult.Available:
                    langAvailable.Add(locale.DisplayLanguage);
                    break;
                case LanguageAvailableResult.CountryAvailable:
                    langAvailable.Add(locale.DisplayLanguage);
                    break;
                case LanguageAvailableResult.CountryVarAvailable:
                    langAvailable.Add(locale.DisplayLanguage);
                    break;
            }
        }



    }

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);

        button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
    }
}

}

`

I need to make an instance to TextToSpeech and it should get two arguments.
One of the arguments is ApplicationContext but the second one is the variable listen but the problem I didn't understand what should I assign to the variable listen since it's null now ?

If I will not make instance for the TextToSpeech and only:

LanguageAvailableResult res = TextToSpeech.IsLanguageAvailable(locale);

Then I will get error on the locale Error 2 An object reference is required for the non-static field, method, or property 'Android.Speech.Tts.TextToSpeech.IsLanguageAvailable(Java.Util.Locale)'

So how do I solve this static error or how do I make instance for the TextToSpeech ?

Answers

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @DanielLip

    In this line:

    var tts = new TextToSpeech(ApplicationContext,listen);

    "listen" should be an instantiated object that implements the TextToSpeech.IOnInitListener interface. This can be your MainActivity class if you like, or any class that implements the methods of TextToSpeech.IOnInitListener (there is only one actually). As an example using the MainActivity class, try:

    public class MainActivity : Activity, TextToSpeech.IOnInitListener
    {
    ...
           public MainActivity()
           {
           ...
           var tts = new TextToSpeech(ApplicationContext,this); // <--- since main activity is now implementing the interface, you can pass "this".  
           ...
           }
    ...
    }</code>

    You will need to actually implement the method required for TextToSpeech.IOnInitListener in the MainActivity class. The only method in that interface is OnInit, so add this method to your MainActivity class ( or whatever class you create that will implement TextToSpeech.IOnInitListener:

    // Interface method required for IOnInitListener
    void TextToSpeech.IOnInitListener.OnInit(OperationResult status)
    {
       // code here
    }

    You can see a sample implementation of this method in our TextToSpeech sample for Android here:
    Guide: http://developer.xamarin.com/guides/android/platform_features/speech/
    Code download: http://developer.xamarin.com/guides/android/platform_features/speech/Resources/TextToSpeech.zip

    I hope this helps!

Sign In or Register to comment.