using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Synthesis;
using System.Globalization;
using System.Threading;

namespace UnNomalized_node
{
    class voice_speaker
    {
        //
        SpeechSynthesizer m_speecher = new SpeechSynthesizer();
        Queue<string> m_voice_text_queue=new Queue<string>();
        Thread m_voice_thread = null;
        bool m_exit;
        public voice_speaker()
        {
            m_exit = false;
            m_voice_thread = new  Thread(speaking);
            m_voice_thread.Start();
        }
        public void release()
        {
           
            m_exit = true;
            if (m_voice_thread.IsAlive)
            {
                m_voice_thread.Abort();
            }
        }

        public void speak(string text)
        {
            
            m_speecher.Volume = 100; //音量
            m_speecher.Rate = -1;
            CultureInfo keyboardCulture = System.Windows.Forms.InputLanguage.CurrentInputLanguage.Culture;
            InstalledVoice neededVoice = m_speecher.GetInstalledVoices(keyboardCulture).FirstOrDefault();
            if (neededVoice == null)
            {
                text = "";
            }
            else
            {
                m_speecher.SelectVoice(neededVoice.VoiceInfo.Name);
            }

            m_speecher.Speak(text);
        }
        void speaking()
        { 
            while (m_exit==false)
            {
                string text = null;
                if (m_voice_text_queue.Count!=0)
                  text= m_voice_text_queue.Dequeue();
                text= "请熄火、拉手刹、全员下车、收后视镜、收天线";
                if (text!=null)
                {
                    speak(text);
                }
                Thread.Sleep(500);
            }
        }
    }
}
