Speech recognition is giving stack overflow

Asked

Viewed 65 times

1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Synthesis; //namespace
using System.IO;
namespace SIF
{
    public class Falador
    {
        private static SpeechSynthesizer sp = new SpeechSynthesizer();
        public static void Falar(string text)
        {
            // caso ele esteja falando
            if (sp.State == SynthesizerState.Speaking)
                sp.SpeakAsyncCancelAll();
            sp.SpeakAsync(text);
        }

        public static void Speak(params string [] texts)
        {
                Random rnd = new Random();
                Speak(texts[rnd.Next(1, texts.Length)]);
        }
    }
}

This code is for my assistant to choose between several words in C# for example:

Falador.Speak("Tudo bem", "Como desejar"); 

then he would choose one of those words and speak but the following error happens:

System.Stackoverflowexception: 'Exception of type 'System.Stackoverflowexception' was thrown.'

How can I solve?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

You are calling the method Speak() within the method Speak(). This is called recursion, so far without problems even if in most cases it is not necessary. Only at some point it needs to stop, otherwise it will go into infinite calls and will burst the execution stack.

I can’t say which solution because the question doesn’t say much, but it may be that I just wanted to make a loop (although I think it would also be a mistake, but I can’t talk without knowing this API).

Most likely it was done by accident, then you’d see what you really want, if you want to call this method recursively, you might want to have another method there. It may even be that you are using the same method name as the API, so you would need to change the name of your own or call the method name of the API in a fully qualified way, ie through the namespace.

Read:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.