Пример #1
0
        void VoiceKeyBoard_Loaded(object sender, RoutedEventArgs e)
        {
            if (KinectSensor.KinectSensors.Count == 0)
            {
                return;
            }
            sensor = KinectSensor.KinectSensors[0];

            try
            {
                //Check if the Sensor is Connected
                if (sensor.Status == KinectStatus.Connected)
                {
                    sensor.Start();

                    // Create and configure speech grammars and recognizer
                    this.OneruleGrammar   = CreateGrammar(Onerule);
                    this.tworuleGrammar   = CreateGrammar(tworule);
                    this.threeruleGrammar = CreateGrammar(threerule);
                    this.fourruleGrammar  = CreateGrammar(fourrule);
                    this.fiveruleGrammar  = CreateGrammar(fiverule);
                    this.sixruleGrammar   = CreateGrammar(sixrule);
                    this.sevenruleGrammar = CreateGrammar(sevenrule);
                    this.eightruleGrammar = CreateGrammar(eightrule);
                    this.nineruleGrammar  = CreateGrammar(ninerule);
                    this.zeroruleGrammar  = CreateGrammar(zerorule);

                    //Delete
                    this.deleteGrammar = CreateGrammar(deleterule);
                    this.clearGrammar  = CreateGrammar(clearrule);
                    this.finishGrammar = CreateGrammar(finishrule);
                    //Submits

                    //recognize the speech
                    this.speechRecognizer = SpeechRecognizer.Create(
                        new[] {
                        OneruleGrammar,
                        tworuleGrammar,
                        threeruleGrammar,
                        fourruleGrammar,
                        fiveruleGrammar,
                        sixruleGrammar,
                        sevenruleGrammar,
                        eightruleGrammar,
                        nineruleGrammar,
                        zeroruleGrammar,
                        deleteGrammar,
                        clearGrammar,
                        finishGrammar
                    });

                    if (null != speechRecognizer)
                    {
                        this.speechRecognizer.SpeechRecognized += SpeechRecognized;

                        this.speechRecognizer.Start(sensor.AudioSource);
                    }
                }
                else if (sensor.Status == KinectStatus.Disconnected)
                {
                    //nice message with Colors to alert you if your sensor is working or not
                    Message.Content    = "Kinect Sensor is not Connected";
                    Message.Background = new SolidColorBrush(Colors.Orange);
                    Message.Foreground = new SolidColorBrush(Colors.Black);
                }
                else if (sensor.Status == KinectStatus.NotPowered)
                {//nice message with Colors to alert you if your sensor is working or not
                    Message.Content    = "Kinect Sensor is not Powered";
                    Message.Background = new SolidColorBrush(Colors.Red);
                    Message.Foreground = new SolidColorBrush(Colors.Black);
                }
                else if (sensor.Status == KinectStatus.NotReady)
                {//nice message with Colors to alert you if your sensor is working or not
                    Message.Content    = "Kinect Sensor is not Ready";
                    Message.Background = new SolidColorBrush(Colors.Red);
                    Message.Foreground = new SolidColorBrush(Colors.Black);
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }