可以拦截线程的声音命令 [英] Sound Command that can intercept the Thread
问题描述
Good Day all
我正在研究Kinect传感器程序,但是我认为WPF论坛可以最好地回答这个问题.
在我的Kinect项目中,用户可以使用Kinect手势或语音命令.手势效果很好,就像使用鼠标一样.我在遵循特定流程的语音命令方面遇到了挑战.请注意,此功能有效 不使用语音命令时效果很好.
让我举一个简单的例子.
我有3个按钮
按钮1 =系统正常"
按钮2 ="查看错误"
按钮3 =系统重启"
第1步:现在,如果用户输入错误的命令,则系统必须播放"Error"(声音)和"Error"(声音)按钮的声音. "查看错误" 必须动画化.
第2步::规则继续进行,说明用户是否未单击 "在5秒钟内查看错误",然后在 "查看错误""
步骤3:并对系统重启" 进行动画处理,然后播放声音 系统重启"
现在,用鼠标单击普通按钮,这将起作用.但是现在我没有物理地单击按钮,我正在使用语音命令并且已经困了 语音识别事件,其定义如下:
私有无效SpeechRecognized(对象发送者,SpeechRecognizedEventArgs e) { //话语置信度,在此之下我们将话语视为未听见 const double ConfidenceThreshold = 0.3; //直角的度数. const int DegreesInRightAngle = 90; //每次乌龟应该向前或向后移动的像素数. const int DisplacementAmount = 60; this.ClearRecognitionHighlights(); 如果(e.Result.Confidence> = ConfidenceThreshold) { 开关(e.Result.Semantics.Value.ToString()) { 情况"VIEWERROR": Error_Escaped = true; btnViewError_Click(null,null); 休息; 案例"SYSTEMREBOOT": btnSystemReboot_Click(null,null); 休息; 情况"SYSTEMSTATUS": SystemStatus_Click(null,null); 休息; } } 别的 { //设置状态文本 this.Status.Content = Properties.Resources.GrammarConfidenceLow; Logger.NormalLog(置信度低,语法不被识别为命令",Generics.Generics.ActivePage); } }
如您所见,我正在以编程方式单击按钮".因此,如果我不点击查看错误",按钮在5秒钟内,系统重启将开始播放另一种声音.
那是关于过程的.现在我面临的挑战是.在根据我的流程使用了错误的选项后,查看错误"按钮将发出声音,而我将发出声音命令"以停止声音,过程"中的下一个仍将被触发. 因此,我调试了代码,并意识到该程序可以运行所有正在运行的代码,并且在完成后,它会击中"SpeechRecognized".事件发生后,我想拦截语音命令并赋予其与单击按钮时相同的功能 鼠标可以做到.
这是一个运行的函数,当出现错误
时私有void ViewError() { Error_Escaped = false; ResetBlinking(this.btnViewError); AnimateButton(this.btnViewError); Generics.Generics.WaitNSeconds(5); 如果(Error_Escaped == false) { SystemReboot(); } }
这将为"Error_Escape = false"设置标志.这意味着没有人单击该按钮.它将对按钮进行动画处理,并等待5秒钟,这是我的自定义间隔计算器.现在,在等待的过程中,我会继续说一些 语音命令停止它.它在我等待时仍会继续,它将跳至系统重启",它会发出声音.我要
语音识别(对象发送者,SpeechRecognizedEventArgs e)
在其他按钮事件被触发后不需要时触发事件.
坦克
武伊斯瓦·马塞科
Vuyiswa Maseko
>>该事件在我不需要触发其他按钮事件后才触发.
我建议您可以尝试定义一个线程来运行您的步骤. SpeechRecognized声音命令可以通过标志拦截线程.
以下代码供您参考.公共静态布尔标志= false; ///< summary> ///定义一个线程以运行三个步骤. ///</summary> 私有void ThreadStart() { 线程P_th = new Thread(()=> { int count = 1; while(标志) { this.Dispatcher.BeginInvoke( System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)(()=> { 如果(count == 5) { button1.Content =系统正常..."; } 否则,如果(计数== 10) { button1.Content =系统正常"; button2.Content =查看错误". } 否则,如果(计数> 15) { button1.Content =系统正常"; button2.Content =查看错误"; button3.Content ="系统重启.......; 标志=假; } })); Thread.Sleep(1000); 数++; } }); P_th.IsBackground = true; P_th.Start(); }
最好的问候,
Yohann Lu
Good Day all
i am working on a Kinect Sensor Program, but i think this question can best be answered by the WPF forum.
In my Kinect Project , the User can use Kinect Gestures or Voice Commands. THe Gestures work nicely its like one is using a mouse. i have a challenge with the Voice Commands which follow a certain Process. Please note that this functionality works well when one is not using voice commands.
Let me make a simple example.
i have a 3 Buttons
Button 1 = "System Ok "
Button 2 = " View Error"
Button 3 = "System Reboot"
Step 1: Now the logic say , if a user enter a wrong command , the System must play a sound for Error and the button for " View Error" must Animate.
Step 2: The Rule goes on and say if the User has not click the " View Error" within 5 seconds , Stop the Animation on the " View Error"
Step 3: and Animate the "System Reboot" and play the Sound for "System Reboot"
Now on a normal button click with a Mouse , this would work. But now i am not clicking the Button Physically, i am using Voice Commands and i have trapped the SpeechRecognized event which is defined like this
private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { // Speech utterance confidence below which we treat speech as if it hadn't been heard const double ConfidenceThreshold = 0.3; // Number of degrees in a right angle. const int DegreesInRightAngle = 90; // Number of pixels turtle should move forwards or backwards each time. const int DisplacementAmount = 60; this.ClearRecognitionHighlights(); if (e.Result.Confidence >= ConfidenceThreshold) { switch (e.Result.Semantics.Value.ToString()) { case "VIEWERROR": Error_Escaped = true; btnViewError_Click(null, null); break; case "SYSTEMREBOOT": btnSystemReboot_Click(null, null); break; case "SYSTEMSTATUS": SystemStatus_Click(null, null); break; } } else { // set the status text this.Status.Content = Properties.Resources.GrammarConfidenceLow; Logger.NormalLog("The Confidence is low , the Grammar is not Recognised as a Command", Generics.Generics.ActivePage) ; } }
as you can see i am clicking the Buttons programmatically. So if i don't click the "View Error" button within 5 seconds , the System Reboot will start playing another sound.
That was about the Process. Now my challenge is that. After i used a wrong option according to my Process, the "View Error" button will give a sound and i will give a Voice Command to Stop the Sound , the next in the Process will still be fired. So i debugged the code and i realised that the Program , runs all the code that was running and when its done, it hit the "SpeechRecognized" event later, i want to intercept and give the Voice Command the same ability as a button click done with a mouse can do.
This is a function that runs , when there is an Error
private void ViewError() { Error_Escaped = false; ResetBlinking(this.btnViewError); AnimateButton(this.btnViewError); Generics.Generics.WaitNSeconds(5); if (Error_Escaped == false) { SystemReboot(); } }
This will set the Flag for "Error_Escape = false " Which means no one has clicked the button. and it will Animate the Button and wait for 5 second which is my custom interval calculator. Now while its waiting i would keep saying some Voice Command to Stop it. it will still continue while i wait , it will jump to System Reboot which will have its sound. i want the
SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
event to fire when i need it not after the other event of buttons are fired.
Tanks
Vuyiswa Maseko
Hi Vuyiswa Maseko,
>>event to fire when i need it not after the other event of buttons are fired.
I suggest you can try to define a thread to run your Steps. The SpeechRecognized Sound Command that can intercept the Thread by the flag.
The following code for your reference.public static bool flag = false; /// <summary> /// define a thread to run three steps. /// </summary> private void ThreadStart() { Thread P_th = new Thread(() => { int count = 1; while (flag) { this.Dispatcher.BeginInvoke( System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)(() => { if (count == 5) { button1.Content = "System Ok ing......"; } else if (count == 10) { button1.Content = "System Ok"; button2.Content = "View Error ing...."; } else if (count >15) { button1.Content = "System Ok"; button2.Content = "View Error"; button3.Content = " System Reboot ing ......."; flag = false; } })); Thread.Sleep(1000); count++; } }); P_th.IsBackground = true; P_th.Start(); }
Best Regards,
Yohann Lu
这篇关于可以拦截线程的声音命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!