Determine Whether a Speech Recognizer Is Running
An application can query Windows to determine whether a speech recognizer is running. The examples in the following sections import the SystemParametersInfo function from User32.dll, to determine whether a system-wide setting has changed. In both examples, if the SystemParametersInfo function has changed a system-wide setting, Windows sends the WM_SETTINGCHANGE message.
Example 1
The first example merely reports that the speech recognizer is running or that it is not. The logic to make this determination is in the IsSpeechRunning method. This method calls SystemParametersInfo, passing the SPI_GETSPEECHRECOGNITION parameter. If the speech recognizer is running, the pvParam parameter in SystemParametersInfo will be set to a nonzero value.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
UpdateLabel();
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_SETTINGCHANGE:
UpdateLabel();
break;
default:
base.WndProc(ref m);
break;
}
}
private void UpdateLabel()
{
if (IsSpeechRunning())
{
label1.Text = "Windows Speech Recognition is RUNNING!";
}
else
{
label1.Text = "Windows Speech Recognition is NOT running!";
}
}
private bool IsSpeechRunning()
{
IntPtr pvParam = (IntPtr)0;
SystemParametersInfo(SPI_GETSPEECHRECOGNITION, 0, ref pvParam, 0);
return pvParam != (IntPtr)0;
}
const int WM_SETTINGCHANGE = 0x1A;
const int SPI_GETSPEECHRECOGNITION = 0x104A;
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(int uiAction, uint uiParam, ref IntPtr pvParam, int fWinIni);
}
}
Example 2
The second example is similar to the first example, by reporting whether the speech recognizer is running or not. The additional feature of this example is that if the speech recognizer is running, the application responds to the user question "What time is it." As is true in the first example, the IsSpeechRunning method calls SystemParametersInfo, passing the SPI_GETSPEECHRECOGNITION parameter. If the speech recognizer is running, the pvParam parameter in SystemParametersInfo will be set to a nonzero value.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
UpdateGrammar();
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_SETTINGCHANGE:
UpdateGrammar();
break;
default:
base.WndProc(ref m);
break;
}
}
private void UpdateGrammar()
{
if (IsSpeechRunning())
{
label1.Text = "Windows Speech Recognition is RUNNING!";
if (sr == null)
{
sr = new SpeechRecognizer();
sr.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sr_SpeechRecognized);
gb = new GrammarBuilder();
gb.Append("What time is it");
sr.LoadGrammar(new Grammar(gb));
}
}
else
{
label1.Text = "Windows Speech Recognition is NOT running!";
if (sr != null)
{
gb = null;
sr.UnloadAllGrammars();
sr.Dispose();
sr = null;
}
}
}
void sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
MessageBox.Show("The time is now " + DateTime.Now.ToShortTimeString());
}
private bool IsSpeechRunning()
{
IntPtr pvParam = (IntPtr)0;
SystemParametersInfo(SPI_GETSPEECHRECOGNITION, 0, ref pvParam, 0);
return pvParam != (IntPtr)0;
}
GrammarBuilder gb;
SpeechRecognizer sr;
const int WM_SETTINGCHANGE = 0x1A;
const int SPI_GETSPEECHRECOGNITION = 0x104A;
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(int uiAction, uint uiParam, ref IntPtr pvParam, int fWinIni);
}
}