[fix]修正UdpServer在接收广播时连续启动接收的错误,在StarAgent中,此时可能收到广播包,SocketFlags是Broadcast,需要清空,否则报错“参考的对象类型不支持尝试的操作”; 无需设置SocketOptionName.PacketInformation,在ReceiveMessageFromAsync时会自动设置,并且支持ipv6;
石头 编写于 2024-10-10 00:36:00 石头 提交于 2024-10-10 00:45:43
X
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Recognition;
using NewLife.Log;
using NewLife.Model;

namespace NewLife.Windows
{
    class MySpeech : ISpeech
    {
        private SpeechRecognitionEngine _rg;

        /// <summary>语音识别事件</summary>
        public event EventHandler<RecognitionEventArgs> SpeechRecognized;

        static MySpeech()
        {
            ObjectContainer.Current.AddSingleton<ISpeech, MySpeech>();
        }

        public Boolean Init()
        {
            try
            {
                var sr = new SpeechRecognitionEngine();

                sr.SetInputToDefaultAudioDevice();
                sr.SpeechRecognized += _rg_SpeechRecognized;
                //sr.RecognizeAsync(RecognizeMode.Multiple);

                _rg = sr;

                return true;
            }
#if DEBUG
            catch (Exception ex)
            {
                XTrace.WriteException(ex);

                return false;
            }
#else
            catch { return false; }
#endif
        }

        public void SetChoices(IEnumerable<String> phrases)
        {
            var gc = _rg.Grammars.Count;
            //_rg.RecognizeAsyncCancel();
            _rg.UnloadAllGrammars();

            var cs = new Choices();
            cs.Add(phrases.ToArray());

            var gb = new GrammarBuilder();
            gb.Append(cs);

            var gr = new Grammar(gb);

            // 不能加载自然语法,否则关键字识别率大大下降
            //_rg.LoadGrammarAsync(new DictationGrammar());
            _rg.LoadGrammarAsync(gr);

            // 首次启动
            if (gc == 0) _rg.RecognizeAsync(RecognizeMode.Multiple);
        }

        void _rg_SpeechRecognized(Object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result == null) return;

            var ev = new RecognitionEventArgs(e.Result.Confidence, e.Result.Text);

            if (SpeechRecognized != null) SpeechRecognized.Invoke(sender, ev);
        }

        public static void Main() { }
    }
}