Question No sound - Virtual Assistant - Visual Studio

romanws

Member
Joined
Mar 10, 2021
Messages
5
Programming Experience
1-3
Hey guys, I´m working on a Virtual Assistant calculator on C# with Visual Stuido. No errors appear but still when I say the assigned activation work nothing happens. I would really appreciate your help.

This is my code...

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Speech.Recognition;
using System.Speech.Synthesis;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SpeechSynthesizer ss = new SpeechSynthesizer();

        SpeechRecognitionEngine ta = new SpeechRecognitionEngine();

        PromptBuilder pb = new PromptBuilder();

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Speak_Click(object sender, EventArgs e)
        {

            ss.SelectVoiceByHints(VoiceGender.Male);

            Choices list = new Choices();

            list.Add(File.ReadAllLines(@"C:\Users\Corporativo Isakar\OneDrive\Escritorio\George\georgecommands"));

            Grammar gr = new Grammar(new GrammarBuilder(list));

            try
            {
                ta.RequestRecognizerUpdate();
                ta.LoadGrammar(gr);
                ta.SpeechRecognized += Ta_SpeechRecognized;
                ta.SetInputToDefaultAudioDevice();
                ta.RecognizeAsync(RecognizeMode.Multiple);

            }
            catch
            {
                return;
            }

            pb.ClearContent();
            pb.AppendText(richTextBox1.Text);
            ss.Speak(pb);

        }

        public void Say(string phrase)
        {

            ss.SpeakAsync(phrase);
        }

        private void Ta_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {

            string speechSaid = e.Result.Text;

            switch (speechSaid)

            {
                case ("hey"):
                    Say("good morning sir");
                    break;
            }


        }

        private void RichTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
 
Last edited by a moderator:
Firstly, please don't post unformatted code. It's not very pleasant to read.

Secondly, please don't post irrelevant code. We don't need to see those namespace imports or those empty event handlers. That just makes it harder for us to work out what actually is relevant to the issue.

In general, if you would like us to volunteer our time and effort to help you, please take the time and make the effort to help us do so, by including everything that is relevant and nothing that isn't and making what you do provide as accessible as possible.
 
when I say the assigned activation work nothing happens
Please point out exactly where in the code you're expecting something to happen and exactly what that something is, as well as what actually does happen. That's not just what happens from the user's perspective but also what the debugger tells you. If you haven't debugged the code, i.e. set a breakpoint, stepped through the code and examined the state of the application at each step, then you need to do that first, so that you can provide us with that relevant information.
 
Please point out exactly where in the code you're expecting something to happen and exactly what that something is, as well as what actually does happen. That's not just what happens from the user's perspective but also what the debugger tells you. If you haven't debugged the code, i.e. set a breakpoint, stepped through the code and examined the state of the application at each step, then you need to do that first, so that you can provide us with that relevant information.
Thank you for your feed back. Im expecting for the code player to answer "good morning sir" whe I say "hey". The code runs perfectly but I don´t get a response when I play and then say "hey". Is that clear? (In general I´m trying for my code to answer to me talking, something like Siri or Alexa.
 
Are you sure that you aren't just silently eating an exception with your lines 54-57? Does you program actually say whatever is in the pb after you click the button?

Next, is the speach recognized event handler actually being called? Have you set a breakpoint there to verify if it being called?
 
The code runs perfectly
Are you absolutely sure about that? Have you debugged and watched the path of execution go exactly as you expect and hit this line:
C#:
ss.SpeakAsync(phrase);
with phrase containing "good morning sir"?
 
Last edited:
Can you post the contents of you georgecommands file? I wanted to test your code myself but can't without that file.

EDIT: I checked the documentation and I can probably just hard-code "hey" there, so I'll try that.
 
I've taken a closer look at your code and it doesn't seem to make sense. Please explain the logical sequence that you're trying to implement. It seems to me that most of what's in Speak_Click should be in Form1_Load because it seems to be initial setup.
 
I've taken a closer look at your code and it doesn't seem to make sense. Please explain the logical sequence that you're trying to implement. It seems to me that most of what's in Speak_Click should be in Form1_Load because it seems to be initial setup.

You´re right, my bad. I have fixed it, the problem rn is my code does not find the document but I´m working on it. Thanks
 
Are you sure that you aren't just silently eating an exception with your lines 54-57? Does you program actually say whatever is in the pb after you click the button?

Next, is the speach recognized event handler actually being called? Have you set a breakpoint there to verify if it being called?
No, it never speaks.
 
No, it never speaks.
Stop approaching this as though you were just a user. You are not. You are a developer, so act accordingly. You don't just run the app and see what it does. You debug the code, using the debugging tools provided in VS, and watch the code and the state of the application as it executes. If you don't know how to debug then you should stop what you're doing and learn, because EVERY developer needs to be able to debug to do anything of use. If you post a question here then we will expect you to have debugged your code thoroughly first so if we ask you whether a specific method gets executed then you need to have actually seen it execute or not in the debugger, not guess what happened based on the behaviour of the UI.
 
Maybe I should try something easier.
That might not be a bad idea if you're not really confident with the basics of programming. Working with speech recognition and synthesis is certainly a fun and interesting topic to explore but you're not doing yourself any favours if you don't know how to structure code and debug it first, because you'll keep making mistakes that have nothing specifically to do with the speech part.
 
Back
Top Bottom