c# Realize text reading, voice alarm function
In the process of testing the software function, due to the need to operate the hardware to change the position, but there is a time delay in viewing the monitoring software after the operation is completed, unless two people cooperate, you want to implement the alarm for voice broadcast function.
Implementation Method 1
[DllImport("winmm. dll”)]
public static extern bool PlaySound(string pszSound, int hmod, int fdwSound);
public const int SND_FILENAME = 0x00020000;
public const int SND_ASYNC = 0x0001;
Then call PlaySound in the place where the alarm is needed to make a sound;
Disadvantages: The wav file must be admitted in advance, and it must be a wav file, which is not flexible
. Method 2
Download the file DotNetSpeech.dll online
and add the reference of this dynamic library to the reference
using DotNetSpeech;//Voice alarm engine
Read function
private void Read(string text)
{ SpVoice sp = new SpVoice(); sp.Rate = 0;//The reading speed can be adjusted by yourself. SpeechVoiceSpeakFlags sFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; sp. Speak(text, sFlags); }
Call the Read function at the place where the alarm is needed to play the content of the parameter;
advantage, no need to record the file, directly read the text content;
Optimization: Since the playback of files is much slower than the text display, voice loss may occur, or the program or the system may be delayed when a large amount of text is played. The thread mode
threadplaysound = new Thread(read);
threadplaysound.IsBackground = true;
threadplaysound. Start();
Add the text
llsound.Add (the text to be added) where the alarm is needed ; //llsound = list
/// <summary>
/// 语音播报 llsount的内容,由于可能未播报完成,有新soe进入引起混乱,由线程调用
/// </summary>
public void read()
{
while(true)
{
while(llsound.Count>0)
{
Read(llsound[0]);
Thread.Sleep(500);
llsound.RemoveAt(0);
}
Thread.Sleep(500);
}
}
The result of the experiment is valid, so mark it.