这是一个可以开机后自动和你打招呼的小程序,至于你的名字在配置文件中。这里主要是应用了微软的speech技术来播放声音。所以如果你有其他的需求可以自己扩展。这里我只是写了很少的一部分功能,以后有新的想法后会不断的添加的。
下面是真材实料,呵呵,用来发音的类,我也是从网上找到的,但是使用起来很简单
下面是配置文件,很简单
---------------------------------------------------------------------------------------------
第一、需要安装speech的sdk和languagepack,这可以从网上找到,我很想上传到我的资源中,但是太大了。
第二、我使用的vs2005,这里需要添加2个com引用,一个是microsoft speech object library;一个是windows script host object model。第一个会包含我们要使用的发音的类,第二个是用来建立快捷方式。
第三、开始写代码,我把主要代码复制出来:
这是唯一的一个窗体的代码,从这里开始执行,窗体上我暂时没有放置任何控件
- public partial class Form1 : Form
- {
- SpeechLibr sp = null;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- // 创建该程序的快捷方式到启动菜单,这样以后我们开机后它就可以自动运行了
- SetThisToStart();
- // 实例化speech类
- sp = SpeechLibr.instance();
- // 说话
- sp.AnalyseSpeak(Utils.SayHelloToMe(Utils.ReadConfigFile()));
- }
- /// <summary>
- /// 创建该程序的快捷方式到启动菜单
- /// </summary>
- private void SetThisToStart()
- {
- WshShell shell = new WshShell();
- string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
- IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(StartupPath + "//brother.lnk");
- shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
- shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
- shortcut.WindowStyle = 1;
- shortcut.Description = "brother";
- shortcut.IconLocation = System.Environment.SystemDirectory + "" + "shell32.dll, 165";
- shortcut.Save();
- }
- }
- public class SpeechLibr
- {
- private static SpeechLibr _Instance = null;
- private SpVoiceClass voice = null;
- private SpeechLibr()
- {
- BuildSpeach();
- }
- private void BuildSpeach()
- {
- if (voice == null)
- voice = new SpVoiceClass();
- }
- public static SpeechLibr instance()
- {
- if (_Instance == null)
- _Instance = new SpeechLibr();
- return _Instance;
- }
- private void SetChinaVoice()
- {
- // ///3表示是汉用,0124都表示英语,就是口音不同
- voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(3);
- }
- private void SetEnglishVoice()
- {
- voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(1);
- }
- private void SpeakChina(string strSpeak)
- {
- SetChinaVoice();
- Speak(strSpeak);
- }
- private void SpeakEnglishi(string strSpeak)
- {
- SetEnglishVoice();
- Speak(strSpeak);
- }
- public void AnalyseSpeak(string strSpeak)
- {
- int iCbeg = 0;
- int iEbeg = 0;
- bool IsChina = true;
- for (int i = 0; i < strSpeak.Length; i++)
- {
- char chr = strSpeak[i];
- if (IsChina)
- {
- if (chr <= 122 && chr >= 65)
- {
- int iLen = i - iCbeg;
- string strValue = strSpeak.Substring(iCbeg, iLen);
- SpeakChina(strValue);
- iEbeg = i;
- IsChina = false;
- }
- }
- else
- {
- if (chr > 122 || chr < 65)
- {
- int iLen = i - iEbeg;
- string strValue = strSpeak.Substring(iEbeg, iLen);
- this.SpeakEnglishi(strValue);
- iCbeg = i;
- IsChina = true;
- }
- }
- }//end for
- if (IsChina)
- {
- int iLen = strSpeak.Length - iCbeg;
- string strValue = strSpeak.Substring(iCbeg, iLen);
- SpeakChina(strValue);
- }
- else
- {
- int iLen = strSpeak.Length - iEbeg;
- string strValue = strSpeak.Substring(iEbeg, iLen);
- SpeakEnglishi(strValue);
- }
- }
- private void BuildSpeech()
- {
- if (voice == null)
- voice = new SpVoiceClass();
- }
- public int Volume
- {
- get
- {
- return voice.Volume;
- }
- set
- {
- voice.SetVolume((ushort)(value));
- }
- }
- public int Rate
- {
- get
- {
- return voice.Rate;
- }
- set
- {
- voice.SetRate(value);
- }
- }
- private void Speak(string strSpeack)
- {
- try
- {
- voice.Speak(strSpeack, SpeechVoiceSpeakFlags.SVSFlagsAsync);
- }
- catch (Exception err)
- {
- throw (new Exception("发生一个错误:" + err.Message));
- }
- }
- public void Stop()
- {
- voice.Speak(string.Empty, SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
- }
- public void Pause()
- {
- voice.Pause();
- }
- public void Continue()
- {
- voice.Resume();
- }
- }
下面是一个工具类,主要提供一些方法来提供想要说的内容和读取配置文件
- public class Utils
- {
- #region ReadConfigFile 读配置文件
- /// <summary>
- /// 读配置文件
- /// </summary>
- public static MeInfo ReadConfigFile()
- {
- MeInfo me = new MeInfo();
- XmlDocument xl = new XmlDocument();
- xl.Load(@"config.xml");
- XmlNodeList xnl = xl.SelectSingleNode("config").ChildNodes;
- foreach (XmlNode xd in xnl)
- {
- XmlElement xe = (XmlElement)xd;
- if (xd.Name == "MyName")
- {
- me.MyName = xe.GetAttribute("value");
- }
- else if (xd.Name == "BirthDay")
- {
- me.BirthDay = xe.GetAttribute("value");
- }
- else if (xd.Name == "Sex")
- {
- me.Sex = xe.GetAttribute("value");
- }
- }
- return me;
- }
- #endregion
- #region SayHelloToMe 向自己打招呼
- /// <summary>
- /// 向自己打招呼
- /// </summary>
- /// <param name="me"></param>
- /// <returns></returns>
- public static string SayHelloToMe(MeInfo me)
- {
- StringBuilder sb = new StringBuilder();
- sb.Append(GetAmOrPm());
- sb.Append("好,");
- sb.Append(me.MyName);
- return sb.ToString();
- }
- #endregion
- #region GetAmOrPm 获得中文的上午或下午
- /// <summary>
- /// 获得中文的上午或下午
- /// </summary>
- /// <returns>午夜,早上,上午,中午,下午,晚上</returns>
- public static string GetAmOrPm()
- {
- if ((DateTime.Now.Hour >= 0) && (DateTime.Now.Hour <5))
- {
- return "午夜";
- }
- else if ((DateTime.Now.Hour >= 5) && (DateTime.Now.Hour < 6))
- {
- return "早上";
- }
- else if ((DateTime.Now.Hour >= 6) && (DateTime.Now.Hour < 12))
- {
- return "上午";
- }
- else if ((DateTime.Now.Hour >= 12) && (DateTime.Now.Hour < 13))
- {
- return "中午";
- }
- else if ((DateTime.Now.Hour >= 12) && (DateTime.Now.Hour < 18))
- {
- return "下午";
- }
- else if ((DateTime.Now.Hour >= 18) && (DateTime.Now.Hour <= 23))
- {
- return "晚上";
- }
- return "";
- }
- #endregion
- }
- <?xml version="1.0" encoding="GB2312"?>
- <config>
- <MyName value="常宇明" />
- <BirthDay value="1980/02/15" />
- <Sex value="男" />
- </config>