using System.Speech.Recognition;namespace Douban
{
public partial class MainWindow : Window
{
SpeechRecognizer speechRecognizer;
public MainWindow()
{
InitializeComponent();
// if (Properties.Settings.Default.EnableSpeech)
speechRecognizer = new SpeechRecognizer();
}private void window_Loaded(object sender, RoutedEventArgs e)
{
// if(Properties.Settings.Default.EnableSpeech)
VoiceCommand();
}private void VoiceCommand()
{
RegisterVoiceCommand(VoiceCommand_Close, "退出");
RegisterVoiceCommand(VoiceCommand_Next, "下一首");
RegisterVoiceCommand(VoiceCommand_Minimize, "最小化");
RegisterVoiceCommand(VoiceCommand_Restore, "恢复");RegisterVoiceCommand(VoiceCommand_Pause, "暂停");
RegisterVoiceCommand(VoiceCommand_Resume, "播放");
}private void VoiceCommand_Resume(object sender, EventArgs e)
{
if (mediaPlayer.MediaState != MediaState.Play)
{
if (mediaPlayer.MediaUri != null)
{
mediaPlayer.Play();
pnlPlayer.SetPlayerStatus(true);
}
}
}private void VoiceCommand_Pause(object sender, EventArgs e)
{
if (mediaPlayer.MediaState == MediaState.Play)
{
mediaPlayer.Pause();
pnlPlayer.SetPlayerStatus(false);
}
}private void VoiceCommand_Close(object sender, EventArgs e)
{
this.Close();
}private void VoiceCommand_Next(object sender, EventArgs e)
{}private void VoiceCommand_Minimize(object sender, EventArgs e)
{
this.WindowState = WindowState.Minimized;
}private void VoiceCommand_Restore(object sender, EventArgs e)
{
this.WindowState = WindowState.Normal;
}private void RegisterVoiceCommand(EventHandler Callback, params string[] Args)
{
Choices choices = new Choices();
foreach (string arg in Args)
{
choices.Add(arg);
}
Grammar grammar = new Grammar(new GrammarBuilder(choices));grammar.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs e)
{
EventHandler eventHandler = new EventHandler(Callback);
Dispatcher.BeginInvoke(eventHandler, sender, e);
};speechRecognizer.LoadGrammarAsync(grammar);
}原帖地址:http://www.jucoder.com/bbs/thread-8299-0-0.html
using System.Speech.Recognition;namespace Douban
{
public partial class MainWindow : Window
{ //创建识别对象
SpeechRecognizer speechRecognizer;
public MainWindow()
{
InitializeComponent();
// if (Properties.Settings.Default.EnableSpeech)
speechRecognizer = new SpeechRecognizer();
} private void window_Loaded(object sender, RoutedEventArgs e)
{
// if(Properties.Settings.Default.EnableSpeech)
VoiceCommand();
}
/// <summary>
/// 注册命令
/// </summary>
private void VoiceCommand()
{
RegisterVoiceCommand(VoiceCommand_Close, "退出");
RegisterVoiceCommand(VoiceCommand_Next, "下一首");
RegisterVoiceCommand(VoiceCommand_Minimize, "最小化");
RegisterVoiceCommand(VoiceCommand_Restore, "恢复"); RegisterVoiceCommand(VoiceCommand_Pause, "暂停");
RegisterVoiceCommand(VoiceCommand_Resume, "播放");
} /// <summary>
/// 恢复播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VoiceCommand_Resume(object sender, EventArgs e)
{
if (mediaPlayer.MediaState != MediaState.Play)
{
//如果当前不是播放状态
if (mediaPlayer.MediaUri != null)
{
//并且当前的播放地址不为空
mediaPlayer.Play();
//设置当前状态为播放状态
pnlPlayer.SetPlayerStatus(true);
}
}
} /// <summary>
/// 暂停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VoiceCommand_Pause(object sender, EventArgs e)
{
if (mediaPlayer.MediaState == MediaState.Play)
{
//如果当前为播放状态,则暂停播放,并设置暂停状态
mediaPlayer.Pause();
pnlPlayer.SetPlayerStatus(false);
}
} /// <summary>
/// 关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VoiceCommand_Close(object sender, EventArgs e)
{
this.Close();
} /// <summary>
/// 下一首
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VoiceCommand_Next(object sender, EventArgs e)
{ } /// <summary>
/// 窗体最小化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VoiceCommand_Minimize(object sender, EventArgs e)
{
this.WindowState = WindowState.Minimized;
} /// <summary>
/// 窗体恢复
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VoiceCommand_Restore(object sender, EventArgs e)
{
this.WindowState = WindowState.Normal;
} /// <summary>
/// 注册命令
/// </summary>
/// <param name="Callback"></param>
/// <param name="Args"></param>
private void RegisterVoiceCommand(EventHandler Callback, params string[] Args)
{
Choices choices = new Choices();
foreach (string arg in Args)
{
choices.Add(arg);
}
Grammar grammar = new Grammar(new GrammarBuilder(choices)); grammar.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs e)
{
EventHandler eventHandler = new EventHandler(Callback);
Dispatcher.BeginInvoke(eventHandler, sender, e);
}; speechRecognizer.LoadGrammarAsync(grammar);
}
}
}