Daniel Lip
Daniel Lip

Reputation: 11319

How do i use text to speech?

I'm using xamarin. And android project. I created a new class and added this code:

using System;
using Android.Speech.Tts;
using System.Collections.Generic;


namespace App1
{

    public class TextToSpeech_Android : Java.Lang.Object, ITextToSpeech, TextToSpeech.IOnInitListener
    {
        TextToSpeech speaker; string toSpeak;
        public TextToSpeech_Android() { }

        public void Speak(string text)
        {
            var c = Forms.Context;
            toSpeak = text;
            if (speaker == null)
            {
                speaker = new TextToSpeech(c, this);
            }
            else
            {
                var p = new Dictionary<string, string>();
                speaker.Speak(toSpeak, QueueMode.Flush, p);
                System.Diagnostics.Debug.WriteLine("spoke " + toSpeak);
            }
        }

        #region IOnInitListener implementation
        public void OnInit(OperationResult status)
        {
            if (status.Equals(OperationResult.Success))
            {
                System.Diagnostics.Debug.WriteLine("speaker init");
                var p = new Dictionary<string, string>();
                speaker.Speak(toSpeak, QueueMode.Flush, p);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("was quiet");
            }
        }
        #endregion
    }

}

I'm getting some errors and warnings:

  1. On the line:

    var c = Forms.Context

Forms not exist. What the Forms do and how can I fix it ?

  1. On the line:

    public class TextToSpeech_Android : Java.Lang.Object, ITextToSpeech, TextToSpeech.IOnInitListener

ITextToSpeech not exist.

And on both same lines:

speaker.Speak(toSpeak, QueueMode.Flush, p);

I'm getting the warning message:

Warning 3 'Android.Speech.Tts.TextToSpeech.Speak(string, Android.Speech.Tts.QueueMode, System.Collections.Generic.IDictionary)' is obsolete: '"deprecated"

What I want to do in my program is that a text I put in a string in my program when I will run my program it will read in voice the text in the string on my phone.

After fixing the errors how do I use the class in the main activity ?

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Speech.Tts;

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



        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++);
            };
        }


    }
}

Upvotes: 0

Views: 1425

Answers (1)

Nicklas Jensen
Nicklas Jensen

Reputation: 1474

You should follow and read every part of the tutorial you're following.

1) Add the following statement to the top of your file (TextToSpeech_Android.cs)

using Xamarin.Forms;

2) Create the ITextToSpeech interface

public interface ITextToSpeech
{
    void Speak (string text);
}

3) That method is deprecated, meaning it might not exists in a future version of Android, and therefor you should use another method.

Upvotes: 1

Related Questions