SoFunction
Updated on 2025-03-06

Example of C# speech recognition usage

This article describes the usage of C# speech recognition. Share it for your reference. The specific analysis is as follows:

C# can use the automatic voice recognition function of Microsoft's operating system to read information. The steps are as follows:

1. Add "" in the project

2. Introduce namespace:

Copy the codeThe code is as follows:
using SpeechLib;

3. Read code:

Copy the codeThe code is as follows:
SpeechVoiceSpeakFlags flag = ;
    SpVoice voice = new SpVoice();
    = (, ).Item(0);
("You are a big beauty, I'll try a handsome guy, haha", flag);

in:

1. SpeechVoiceSpeakFlags is an enum.

SpVoice logo:
    SVSFDefault = 0
    SVSFlagsAsync = 1
    SVSFPurgeBeforeSpeak = 2
    SVSFIsFilename = 4
    SVSFIsXML = 8
    SVSFIsNotXML = 16
    SVSFPersistXML = 32

Regularization logo:
    SVSFNLPSpeakPunc = 64

mask:
    SVSFNLPMask = 64
    SVSFVoiceMask = 127
    SVSFUnusedFlags = -128 

End the Enumeration Molecule

SVSFDefault
Specifies the default settings that should be used. The default values ​​are:
Determined text string synchronization (overrides with SVSFlagsAsync),
Not clearing pending speech requests (overrides with SVSFPurgeBeforeSpeak),
To parse XML text, if the first character is left angle bracket (overridden with SVSFIsXML or SVSFIsNotXML),
Not sticking to global XML state changes in talk calls (overriding SVSFPersistXML),
Unextended punctuation characters into characters (overrides with SVSFNLPSpeakPunc).
SVSFlagsAsync
The specified call should be asynchronous. That is, it will immediately return to the queued request after the request.
SVSFPurgeBeforeSpeak
Clear all pending speech requests, calls spoken before this.
SVSFIsFilename
The string passed by the speaking method is a file name, not a literal. As a result, there is no speech string itself but the path to the file, which is verbal processing.
SVSFIsXML
The entered text will be parsed into XML tags.
SVSFIsNotXML
The entered text will not be parsed XML tags.
SVSFPersistXML
Changes in the global state of the XML tag will continue to the talking call.
SVSFNLPSpeakPunc
, punctuation marks should be expanded to the words (for example: "What is this." will become "This is this issue").
SVSFNLPMask
SAPI (rather than text-to-speech engine) handles flags in this mask.
SVSFVoiceMask
This mask has each logo setting.
SVSFUnusedFlags
This mask has every unused bit set.

2. SpVoice

The SpVoice class is a core class that supports speech synthesis (TTS). Calling the TTS engine through the SpVoice object, thereby realizing the reading function.

The SpVoice class has the following main properties:

Voice: represents the pronunciation type, which is equivalent to the person who reads aloud, including four types: Microsoft Mary, Microsoft Microsoft, Microsoft Sam and Microsoft Simplified Chinese. The first three can only read in English, and the last can be read in Chinese or English, but for English words, you can only read the letters included in them one by one. In the following program, we will find a way to solve this problem.
Rate: The speed of speech reading, the value range is -10 to +10. The larger the value, the faster the speed.
Volume: Volume, value range from 0 to 100. The higher the value, the higher the volume.
SpVoice has the following main methods:
Speak: Completely convert text information into speech and read aloud according to the specified parameters. This method has two parameters: Text and Flags, which specifies the text to be read and the reading method (synchronous or asynchronous, etc.).
Pause: Pause all reading processes using this object. This method has no parameters.
Resume: Restores the paused reading process corresponding to the object. This method has no parameters.

I hope this article will be helpful to everyone's C# programming.