一个可以开机后自动和你打招呼的小程序(希望以后可以成为智能的系统)

这是一个可以开机后自动和你打招呼的小程序,至于你的名字在配置文件中。这里主要是应用了微软的speech技术来播放声音。所以如果你有其他的需求可以自己扩展。这里我只是写了很少的一部分功能,以后有新的想法后会不断的添加的。
---------------------------------------------------------------------------------------------
第一、需要安装speech的sdk和languagepack,这可以从网上找到,我很想上传到我的资源中,但是太大了。
第二、我使用的vs2005,这里需要添加2个com引用,一个是microsoft speech object library;一个是windows script host object model。第一个会包含我们要使用的发音的类,第二个是用来建立快捷方式。
第三、开始写代码,我把主要代码复制出来:
这是唯一的一个窗体的代码,从这里开始执行,窗体上我暂时没有放置任何控件
  1. public partial class Form1 : Form
  2.     {
  3.         SpeechLibr sp = null;
  4.         public Form1()
  5.         {
  6.             InitializeComponent();
  7.         }
  8.         private void Form1_Load(object sender, EventArgs e)
  9.         {
  10.             // 创建该程序的快捷方式到启动菜单,这样以后我们开机后它就可以自动运行了
  11.             SetThisToStart();
  12.             // 实例化speech类
  13.             sp = SpeechLibr.instance();
  14.             // 说话
  15.             sp.AnalyseSpeak(Utils.SayHelloToMe(Utils.ReadConfigFile()));
  16.         }
  17.         /// <summary>
  18.         /// 创建该程序的快捷方式到启动菜单
  19.         /// </summary>
  20.         private void SetThisToStart()
  21.         {
  22.             WshShell shell = new WshShell();
  23.             string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
  24.             IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(StartupPath + "//brother.lnk");
  25.             shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
  26.             shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
  27.             shortcut.WindowStyle = 1;
  28.             shortcut.Description = "brother";
  29.             shortcut.IconLocation = System.Environment.SystemDirectory + "" + "shell32.dll, 165";
  30.             shortcut.Save();
  31.         }
  32.     }
下面是真材实料,呵呵,用来发音的类,我也是从网上找到的,但是使用起来很简单
  1. public class SpeechLibr
  2.     {
  3.         private static SpeechLibr _Instance = null;
  4.         private SpVoiceClass voice = null;
  5.         private SpeechLibr()
  6.         {
  7.             BuildSpeach();
  8.         }
  9.         private void BuildSpeach()
  10.         {
  11.             if (voice == null)
  12.                 voice = new SpVoiceClass();
  13.         }
  14.         public static SpeechLibr instance()
  15.         {
  16.             if (_Instance == null)
  17.                 _Instance = new SpeechLibr();
  18.             return _Instance;
  19.         }
  20.         private void SetChinaVoice()
  21.         {
  22.             // ///3表示是汉用,0124都表示英语,就是口音不同
  23.             voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(3);
  24.         }
  25.         private void SetEnglishVoice()
  26.         {
  27.             voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(1);
  28.         }
  29.         private void SpeakChina(string strSpeak)
  30.         {
  31.             SetChinaVoice();
  32.             Speak(strSpeak);
  33.         }
  34.         private void SpeakEnglishi(string strSpeak)
  35.         {
  36.             SetEnglishVoice();
  37.             Speak(strSpeak);
  38.         }
  39.         public void AnalyseSpeak(string strSpeak)
  40.         {
  41.             int iCbeg = 0;
  42.             int iEbeg = 0;
  43.             bool IsChina = true;
  44.             for (int i = 0; i < strSpeak.Length; i++)
  45.             {
  46.                 char chr = strSpeak[i];
  47.                 if (IsChina)
  48.                 {
  49.                     if (chr <= 122 && chr >= 65)
  50.                     {
  51.                         int iLen = i - iCbeg;
  52.                         string strValue = strSpeak.Substring(iCbeg, iLen);
  53.                         SpeakChina(strValue);
  54.                         iEbeg = i;
  55.                         IsChina = false;
  56.                     }
  57.                 }
  58.                 else
  59.                 {
  60.                     if (chr > 122 || chr < 65)
  61.                     {
  62.                         int iLen = i - iEbeg;
  63.                         string strValue = strSpeak.Substring(iEbeg, iLen);
  64.                         this.SpeakEnglishi(strValue);
  65.                         iCbeg = i;
  66.                         IsChina = true;
  67.                     }
  68.                 }
  69.             }//end for 
  70.             if (IsChina)
  71.             {
  72.                 int iLen = strSpeak.Length - iCbeg;
  73.                 string strValue = strSpeak.Substring(iCbeg, iLen);
  74.                 SpeakChina(strValue);
  75.             }
  76.             else
  77.             {
  78.                 int iLen = strSpeak.Length - iEbeg;
  79.                 string strValue = strSpeak.Substring(iEbeg, iLen);
  80.                 SpeakEnglishi(strValue);
  81.             }
  82.         }
  83.         private void BuildSpeech()
  84.         {
  85.             if (voice == null)
  86.                 voice = new SpVoiceClass();
  87.         }
  88.         public int Volume
  89.         {
  90.             get
  91.             {
  92.                 return voice.Volume;
  93.             }
  94.             set
  95.             {
  96.                 voice.SetVolume((ushort)(value));
  97.             }
  98.         }
  99.         public int Rate
  100.         {
  101.             get
  102.             {
  103.                 return voice.Rate;
  104.             }
  105.             set
  106.             {
  107.                 voice.SetRate(value);
  108.             }
  109.         }
  110.         private void Speak(string strSpeack)
  111.         {
  112.             try
  113.             { 
  114.                 voice.Speak(strSpeack, SpeechVoiceSpeakFlags.SVSFlagsAsync);
  115.             }
  116.             catch (Exception err)
  117.             {
  118.                 throw (new Exception("发生一个错误:" + err.Message));
  119.             }
  120.         }
  121.         public void Stop()
  122.         {
  123.             voice.Speak(string.Empty, SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
  124.         }
  125.         public void Pause()
  126.         {
  127.             voice.Pause();
  128.         }
  129.         public void Continue()
  130.         {
  131.             voice.Resume();
  132.         } 
  133.     }

下面是一个工具类,主要提供一些方法来提供想要说的内容和读取配置文件
  1. public class Utils
  2.     {
  3.         #region ReadConfigFile 读配置文件
  4.         /// <summary>
  5.         /// 读配置文件
  6.         /// </summary>
  7.         public static MeInfo ReadConfigFile()
  8.         {
  9.             MeInfo me = new MeInfo();
  10.             XmlDocument xl = new XmlDocument();
  11.             xl.Load(@"config.xml");
  12.             XmlNodeList xnl = xl.SelectSingleNode("config").ChildNodes;
  13.             foreach (XmlNode xd in xnl)
  14.             {
  15.                 XmlElement xe = (XmlElement)xd;
  16.                 if (xd.Name == "MyName")
  17.                 {
  18.                     me.MyName = xe.GetAttribute("value");
  19.                 }
  20.                 else if (xd.Name == "BirthDay")
  21.                 {
  22.                     me.BirthDay = xe.GetAttribute("value");
  23.                 }
  24.                 else if (xd.Name == "Sex")
  25.                 {
  26.                     me.Sex = xe.GetAttribute("value");
  27.                 }
  28.             }
  29.             return me;
  30.         }
  31.         #endregion
  32.         #region SayHelloToMe 向自己打招呼
  33.         /// <summary>
  34.         /// 向自己打招呼
  35.         /// </summary>
  36.         /// <param name="me"></param>
  37.         /// <returns></returns>
  38.         public static string SayHelloToMe(MeInfo me) 
  39.         {
  40.             StringBuilder sb = new StringBuilder();
  41.             sb.Append(GetAmOrPm());
  42.             sb.Append("好,");
  43.             sb.Append(me.MyName);
  44.             return sb.ToString();
  45.         }
  46.         #endregion
  47.         #region GetAmOrPm 获得中文的上午或下午
  48.         /// <summary>
  49.         /// 获得中文的上午或下午
  50.         /// </summary>
  51.         /// <returns>午夜,早上,上午,中午,下午,晚上</returns>
  52.         public static string GetAmOrPm() 
  53.         {
  54.             if ((DateTime.Now.Hour >= 0) && (DateTime.Now.Hour <5))
  55.             {
  56.                 return "午夜";
  57.             }
  58.             else if ((DateTime.Now.Hour >= 5) && (DateTime.Now.Hour < 6))
  59.             {
  60.                 return "早上";
  61.             }
  62.             else if ((DateTime.Now.Hour >= 6) && (DateTime.Now.Hour < 12))
  63.             {
  64.                 return "上午";
  65.             }
  66.             else if ((DateTime.Now.Hour >= 12) && (DateTime.Now.Hour < 13))
  67.             {
  68.                 return "中午";
  69.             }
  70.             else if ((DateTime.Now.Hour >= 12) && (DateTime.Now.Hour < 18))
  71.             {
  72.                 return "下午";
  73.             }
  74.             else if ((DateTime.Now.Hour >= 18) && (DateTime.Now.Hour <= 23))
  75.             {
  76.                 return "晚上";
  77.             }
  78.             return "";
  79.         }
  80.         #endregion
  81.     }
下面是配置文件,很简单
  1. <?xml version="1.0" encoding="GB2312"?>
  2. <config>
  3.   <MyName value="常宇明" />
  4.   <BirthDay value="1980/02/15" />
  5.   <Sex value="男" />
  6. </config>





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值