Search code examples
c#text-to-speechvoice

Speaking software using C#.net?


How to create C# code that would allow my computer to speak english words that would be based on text provided by the system?


Solution

  • you can try to use this code of mine.

    public static class Melodie
    {
        private static SpeechLib.SpVoice WomenAgent = new SpeechLib.SpVoice();
    
        public static void AnnounceRestrictionOfAccount()
        {
            WomenAgent.Speak("You're account has been block by the system security", SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault);
        }
    
        public static void SayGoodBye()
        {
            WomenAgent.Speak("Goodbye!");
        }
    
        public static void WelcomeUser(User userToBeWelcomed)
        {
            string Salutation = ConstructWelcomeSpeech(userToBeWelcomed);
            WomenAgent.Speak(Salutation);
        }
    
        private static string ConstructWelcomeSpeech(User user)
        {
            string salutation = "Welcome ";
            if (user.Gender == "Male")
            {
                salutation += " Mr. ";
            }
            else if (user.Gender == "Female")
            {
                if (user.CivilStatus != null)
                {
                    if (user.CivilStatus == "Single")
                        salutation += " Ms. ";
                    else
                        salutation += " Mrs. ";
                }
            }
            salutation += user.FirstName + " " + user.LastName;
            return salutation;
        }
    
        public static void AnnounceMessage(string message)
        {
            WomenAgent.Speak(message);
        }
    }
    

    and you can read more about SpeechLib in MSDN.

    another thing you should also include Microsoft speech lib 5.0 as a reference in you're project. :)