大家好,又见面了,我是你们的朋友全栈君。
七、我的菜单
右键点击角色是不是会弹出一个菜单?什么,只有 Hide 一项?想不想定义一个个性的菜单呢?
js 代码
- <object style=“visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F”></object>
- <Script Language=“JavaScript” For=“MSAgent” Event=“Command(UserInput)”>
- switch(UserInput.Name) {
- case “INTRO” :
- Agent.Play(“Explain”);
- Agent.Speak(“My name is “ AgentID “, I think I’m the best one!”);
- break;
- case “AUTHOR”:
- Agent.Play(“Announce”);
- Agent.Speak(“Windy_sk <windy_sk@126.com> wrote the program, I think he’s great! (^o^)”);
- break;
- case “SAYTIME”:
- Agent.Play(“Suggest”);
- Agent.Speak(“It is now “ (new Date()) “!”);
- break;
- case “FLY”:
- Agent.MoveTo(Math.round(Math.random() * screen.width), Math.round(Math.random() * screen.height));
- break;
- case “STOP”:
- Agent.StopAll();
- Agent.Play(“RestPose”);
- break;
- default:
- break;
- }
- </Script>
- <script language=“JavaScript”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- var Agent = null;
- var AgentID, AgentACS;
- var AgentLoad = false;
- function LoadAgent(NewAgent) {
- var remote = false;
- if(AgentLoad) {
- MSAgent.Characters.Unload(AgentID);
- MSAgent.Connected = false;
- Agent = null;
- }
- AgentID = NewAgent;
- AgentACS = NewAgent “.acs”;
- MSAgent.Connected = true;
- try {
- MSAgent.Characters.Load(AgentID, AgentACS);
- window.status = “Local MSAgent load successfully!”;
- } catch(e) {
- AgentACS = “http://agent.microsoft.com/agent2/chars/” NewAgent “/” NewAgent “.acf”;
- remote = true;
- MSAgent.Characters.Load(AgentID, AgentACS);
- window.status = “Local MSAgent load unsuccessfully, as a advice, you’d better to download the charactor file to your local disk!”;
- }
- AgentLoad = true;
- Agent = MSAgent.Characters.Character(AgentID);
- Agent.LanguageID = 0x0409;
- Agent.Commands.RemoveAll();
- Agent.Commands.Visible = true;
- Agent.Commands.Caption = “MSAgent’s Menu – by windy_sk”;
- Agent.Commands.Add(“INTRO”, “Introduce Yourself”, “Introduce yourself”);
- Agent.Commands.Add(“AUTHOR”, “Who Write The Program”, “Who Write The Program”);
- Agent.Commands.Add(“SAYTIME”, “What Time Is It Now”, “What Time Is It Now”);
- Agent.Commands.Add(“FLY”, “Can You Fly”, “Can You Fly”);
- Agent.Commands.Add(“STOP”, “Stop All Actions”, “Stop All Actions”);
- if(remote) {
- Agent.get(“state”, “Showing, Hiding”);
- Agent.get(“animation”, “Explain, Announce, Suggest”);
- }
- Agent.MoveTo(400, 300);
- Agent.Show();
- return;
- }
- LoadAgent(“Merlin”);
- </script>
- MSAgent Select :
- <SELECT name=“Agent_select” οnchange=“LoadAgent(this[this.selectedIndex].text)”>
- <OPTION>Merlin</OPTION>
- <OPTION>Peedy</OPTION>
- <OPTION>Genie</OPTION>
- <OPTION>Robby</OPTION>
- </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 代码
- <Script Language=“JavaScript” For=“MSAgent” Event=“Command(UserInput)”>
- var BadConfidence = 10;
- if (UserInput.Confidence <= -40){
- alert(“Bad Recognition!”);
- } else if ((UserInput.Alt1Name != “”) && (Math.abs(Math.abs(UserInput.Alt1Confidence) – Math.abs(UserInput.Confidence)) < BadConfidence)) {
- alert(“Bad Confidence – too close to another command !”);
- } else if ((UserInput.Alt2Name != “”) && (Math.abs(Math.abs(UserInput.Alt1Confidence) – Math.abs(UserInput.Confidence)) < BadConfidence)) {
- alert(“Bad Confidence – too close to another command !”);
- } else {
- switch(UserInput.Name) {
- case “ACO” :
- MSAgent.PropertySheet.Visible = true;
- break;
- case “READ”:
- Agent_Show(“Read”);
- Agent.Speak(show.value);
- break;
- case “SAYTIME”:
- Agent_Show(“Suggest”);
- Agent.Speak(“It is now “ (new Date()) “!”);
- break;
- case “INTRO” :
- Agent_Show(“Explain”);
- Agent.Speak(“My name is “ AgentID “, I think I’m the best one!”);
- break;
- case “AUTHOR”:
- Agent_Show(“Announce”);
- Agent.Speak(“Windy_sk <windy_sk@126.com> wrote the program, I think he’s great! (^o^)”);
- break;
- case “FLY”:
- Agent.MoveTo(Math.round(Math.random() * screen.width – Agent.width), Math.round(Math.random() * screen.height – Agent.height));
- break;
- case “STOP”:
- Agent.StopAll();
- Agent_Show(“RestPose”);
- break;
- default:
- break;
- }
- }
- </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