MSAgent 详细解说(下)

2022-09-07 10:43:04 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

七、我的菜单

右键点击角色是不是会弹出一个菜单?什么,只有 Hide 一项?想不想定义一个个性的菜单呢?

js 代码

  1. <object style=“visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F”></object>
  2. <Script Language=“JavaScript” For=“MSAgent” Event=“Command(UserInput)”>
  3. switch(UserInput.Name) {
  4. case “INTRO” :
  5. Agent.Play(“Explain”);
  6. Agent.Speak(“My name is “ AgentID “, I think I’m the best one!”);
  7. break;
  8. case “AUTHOR”:
  9. Agent.Play(“Announce”);
  10. Agent.Speak(“Windy_sk <windy_sk@126.com> wrote the program, I think he’s great! (^o^)”);
  11. break;
  12. case “SAYTIME”:
  13. Agent.Play(“Suggest”);
  14. Agent.Speak(“It is now “ (new Date()) “!”);
  15. break;
  16. case “FLY”:
  17. Agent.MoveTo(Math.round(Math.random() * screen.width), Math.round(Math.random() * screen.height));
  18. break;
  19. case “STOP”:
  20. Agent.StopAll();
  21. Agent.Play(“RestPose”);
  22. break;
  23. default:
  24. break;
  25. }
  26. </Script>
  27. <script language=“JavaScript”>
  28. //Coded by Windy_sk <windy_sk@126.com> 20040214
  29. var Agent = null;
  30. var AgentID, AgentACS;
  31. var AgentLoad = false;
  32. function LoadAgent(NewAgent) {
  33. var remote = false;
  34. if(AgentLoad) {
  35. MSAgent.Characters.Unload(AgentID);
  36. MSAgent.Connected = false;
  37. Agent = null;
  38. }
  39. AgentID = NewAgent;
  40. AgentACS = NewAgent “.acs”;
  41. MSAgent.Connected = true;
  42. try {
  43. MSAgent.Characters.Load(AgentID, AgentACS);
  44. window.status = “Local MSAgent load successfully!”;
  45. } catch(e) {
  46. AgentACS = “http://agent.microsoft.com/agent2/chars/” NewAgent “/” NewAgent “.acf”;
  47. remote = true;
  48. MSAgent.Characters.Load(AgentID, AgentACS);
  49. window.status = “Local MSAgent load unsuccessfully, as a advice, you’d better to download the charactor file to your local disk!”;
  50. }
  51. AgentLoad = true;
  52. Agent = MSAgent.Characters.Character(AgentID);
  53. Agent.LanguageID = 0x0409;
  54. Agent.Commands.RemoveAll();
  55. Agent.Commands.Visible = true;
  56. Agent.Commands.Caption = “MSAgent’s Menu – by windy_sk”;
  57. Agent.Commands.Add(“INTRO”, “Introduce Yourself”, “Introduce yourself”);
  58. Agent.Commands.Add(“AUTHOR”, “Who Write The Program”, “Who Write The Program”);
  59. Agent.Commands.Add(“SAYTIME”, “What Time Is It Now”, “What Time Is It Now”);
  60. Agent.Commands.Add(“FLY”, “Can You Fly”, “Can You Fly”);
  61. Agent.Commands.Add(“STOP”, “Stop All Actions”, “Stop All Actions”);
  62. if(remote) {
  63. Agent.get(“state”, “Showing, Hiding”);
  64. Agent.get(“animation”, “Explain, Announce, Suggest”);
  65. }
  66. Agent.MoveTo(400, 300);
  67. Agent.Show();
  68. return;
  69. }
  70. LoadAgent(“Merlin”);
  71. </script>
  72. MSAgent Select :
  73. <SELECT name=“Agent_select” οnchange=“LoadAgent(this[this.selectedIndex].text)”>
  74. <OPTION>Merlin</OPTION>
  75. <OPTION>Peedy</OPTION>
  76. <OPTION>Genie</OPTION>
  77. <OPTION>Robby</OPTION>
  78. </SELECT>

个性菜单也是通过对象事件驱动的,这一点相信已经看懂了上一节的读者比较容易理解,相关网页请见 msdn.microsoft.com/library/en-us/msagent/pacontrol_8kfe.asp

Agent.Commands.Add 的三个属性分别为:内部索引标识、显示文字和语音表示,前两个参数比较容易理解,关键是第三个参数“语音表示”,对用户可以通过语音来控制 MSAgent !

你需要首先下载 MicroSoft 语音识别引擎 activex.microsoft.com/activex/controls/agent2/actcnc.exe (目前只支持美国英语),下面介绍一下语音参数的句法:

Agent.Commands.Add(“SAYTIME”, “What Time Is It Now”, “What Time Is It Now” )中 “What Time Is It Now” 是它的语音表示,但是必须要完全读出这几个单词才可以识别吗?如果要是问时间的话,我可以说 What Time , Tell me the time 或者直接只说 time ,但是如何让程序识别这些话为统一目的呢? 可以这样写:[(What|Tell me the)] Time [is it] [Now] [please] ,熟悉一点正则的读者应该不难理解,[] 表示可选项, () 规定范围, | 表示逻辑或

单纯这样还不够,还要在 <Script Language=”JavaScript” For=”MSAgent” Event=”Command(UserInput)”> 加上相应的声音处理:

js 代码

  1. <Script Language=“JavaScript” For=“MSAgent” Event=“Command(UserInput)”>
  2. var BadConfidence = 10;
  3. if (UserInput.Confidence <= -40){
  4. alert(“Bad Recognition!”);
  5. } else if ((UserInput.Alt1Name != “”) && (Math.abs(Math.abs(UserInput.Alt1Confidence) – Math.abs(UserInput.Confidence)) < BadConfidence)) {
  6. alert(“Bad Confidence – too close to another command !”);
  7. } else if ((UserInput.Alt2Name != “”) && (Math.abs(Math.abs(UserInput.Alt1Confidence) – Math.abs(UserInput.Confidence)) < BadConfidence)) {
  8. alert(“Bad Confidence – too close to another command !”);
  9. } else {
  10. switch(UserInput.Name) {
  11. case “ACO” :
  12. MSAgent.PropertySheet.Visible = true;
  13. break;
  14. case “READ”:
  15. Agent_Show(“Read”);
  16. Agent.Speak(show.value);
  17. break;
  18. case “SAYTIME”:
  19. Agent_Show(“Suggest”);
  20. Agent.Speak(“It is now “ (new Date()) “!”);
  21. break;
  22. case “INTRO” :
  23. Agent_Show(“Explain”);
  24. Agent.Speak(“My name is “ AgentID “, I think I’m the best one!”);
  25. break;
  26. case “AUTHOR”:
  27. Agent_Show(“Announce”);
  28. Agent.Speak(“Windy_sk <windy_sk@126.com> wrote the program, I think he’s great! (^o^)”);
  29. break;
  30. case “FLY”:
  31. Agent.MoveTo(Math.round(Math.random() * screen.width – Agent.width), Math.round(Math.random() * screen.height – Agent.height));
  32. break;
  33. case “STOP”:
  34. Agent.StopAll();
  35. Agent_Show(“RestPose”);
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. </Script>

else 语句上面的部分是对用户的语音输入作判断,相关参数意义请见这里:http://msdn.microsoft.com/library/en-us/dnexpvb/html/usingmsagentcontrolevents.asp

有关 Command 对象的其他方法,请见 msdn.microsoft.com/library/en-us/dnexpvb/html/workingwithcommands.asp

八 归纳总结

主要涉及到的基本方法都已经讲过了,是不是对 MSAgent 又有了一个新的认识?感觉现在写程序的话缺少的仅仅是创意了……

下面,我们就把上面所了解到的东西做一个归纳总结性质的实践,Charactors From Office 是从硬盘上搜索出的 *.acs 文件 copy 到 %windows%MSAgentChars 目录下的角色文件,读者自己搜索添加吧,里面的功能有一些前文没有涉及到的,读者可以研究一下:

此处代码参见附件

算是个所有方法的大杂烩了,最后说明一下 Balloon 对象,其所有属性中只有 Style 可以更改,而官方所给的资料还不完全 msdn.microsoft.com/library/en-us/msagent/pacontrol_9gtm.asp ,还是自己慢慢挖掘吧……

到这里就算是告一段落吧,不想写了,总感觉看的越多就越不了解,真的开始有点佩服 MicroSoft 了,单这么个冰山一角就涉及到无数的枝节,本文虽然不算很彻底的研究,但是入门级已经是绰绰有余了……

资源:

StartPage: msdn.microsoft.com/library/en-us/msagent/agentstartpage_7gdh.asp FAQ: msdn.microsoft.com/library/en-us/msagent/paface_3sit.asp Methods: msdn.microsoft.com/library/en-us/msagent/paface_73c5.asp Events: msdn.microsoft.com/library/en-us/msagent/paface_2xet.asp Actions: msdn.microsoft.com/library/en-us/msagent/deschar_3pgy.asp LanguageId: www.microsoft.com/globaldev/reference/oslocversion.mspx Balloon: msdn.microsoft.com/library/en-us/msagent/pacontrol_9gtm.asp Voice: msdn.microsoft.com/library/en-us/dnexpvb/html/usingmsagentcontrolevents.asp Command: msdn.microsoft.com/library/en-us/dnexpvb/html/workingwithcommands.asp Download: www.microsoft.com/msagent/downloads/user.asp www.microsoft.com/msagent/downloads/developer.asp www.msagentring.org/chars.htm msagentworld.tripod.com/characters.htm

终结:

本文仅是通过我个人对 MSAgent 的理解,以及以前的操作实践写成的,难免有所缺憾,随时欢迎补充和更正!

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147953.html原文链接:https://javaforall.cn

0 人点赞