C# for Programmers, 2/e
 |
ISBN:
0-13-134591-5
© 2006
pages: ~1350
Order
|
This tutorial introduces Microsoft Agenta technology for enhancing Windows applications and Web pages with interactive animated characters that can speak to users and respond to user input via speech synthesis and speech recognition.
[Note: This tutorial (Part 1, Part 2, Part 3, Part 4, Part 5, Part 6, Part 7) is an excerpt (Section 17.14) of Chapter 17, Multimedia and Graphics, from our book C# for Programmers, 2/e. These articles may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C# FOR PROGRAMMERS, ©2005, pp.692–706. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
Introduction to Microsoft Agent (continued)
Creating an Application That Uses Microsoft Agent
[
Note: Before running this example, you must first download and install the Microsoft Agent control, a speech-recognition engine, a text-to-speech engine and the four character definitions from the Microsoft Agent Web site, as we discussed at the beginning of this section.]
The following example (Fig. 17.37) demonstrates how to build a simple application with the Microsoft Agent control. This application contains two drop-down lists from which the user can choose an Agent character and a character animation. When the user chooses from these lists, the chosen character appears and performs the selected animation. The application uses speech recognition and synthesis to control the character animations and speech-you can tell the character which animation to perform by pressing the
Scroll Lock key, then speaking the animation name into a microphone.
Fig. 17.37
Microsoft Agent demonstration.
|
|
|
|
|
|
4 using System.Collections;
|
5 using System.Windows.Forms;
|
|
|
|
|
8 public partial class Agent : Form
|
|
|
|
|
11 private AgentObjects.IAgentCtlCharacter speaker;
|
|
|
|
|
|
|
|
|
16 InitializeComponent();
|
|
|
|
|
|
|
|
|
|
|
22 mainAgent.Characters.Load( "Genie",
|
23 @ "C:\windows\msagent\chars\Genie.acs" );
|
24 mainAgent.Characters.Load( "Merlin",
|
25 @ "C:\windows\msagent\chars\Merlin.acs" );
|
26 mainAgent.Characters.Load( "Peedy",
|
27 @ "C:\windows\msagent\chars\Peedy.acs" );
|
28 mainAgent.Characters.Load( "Robby",
|
29 @ "C:\windows\msagent\chars\Robby.acs" );
|
|
|
|
|
32 speaker = mainAgent.Characters[ "Genie" ];
|
|
|
|
|
35 characterCombo.SelectedText = "Genie";
|
|
|
37 catch ( FileNotFoundException )
|
|
|
39 MessageBox.Show( "Invalid character location",
|
40 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
|
|
|
|
|
|
|
|
|
45 private void speakButton_Click( object sender, EventArgs e )
|
|
|
|
|
|
|
|
|
50 if ( speechTextBox.Text == "" )
|
|
|
52 "Please, type the words you want me to speak", "" );
|
|
|
54 speaker.Speak( speechTextBox.Text, "" );
|
|
|
|
|
|
|
58 private void mainAgent_ClickEvent(
|
59 object sender, AxAgentObjects._AgentEvents_ClickEvent e )
|
|
|
61 speaker.Play( "Confused" );
|
62 speaker.Speak( "Why are you poking me?", "" );
|
63 speaker.Play( "RestPose" );
|
|
|
|
|
|
|
67 private void characterCombo_SelectedIndexChanged(
|
68 object sender, EventArgs e )
|
|
|
70 ChangeCharacter( characterCombo.Text );
|
|
|
|
|
|
|
74 private void ChangeCharacter( string name )
|
|
|
76 speaker.StopAll( "Play" );
|
|
|
78 speaker = mainAgent.Characters[ name ];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 private void GetAnimationNames()
|
|
|
|
|
|
|
|
|
|
|
92 IEnumerator enumerator = mainAgent.Characters[
|
93 speaker.Name ].AnimationNames.GetEnumerator();
|
|
|
|
|
|
|
|
|
98 actionsCombo.Items.Clear();
|
99 speaker.Commands.RemoveAll();
|
|
|
|
|
102 while ( enumerator.MoveNext() )
|
|
|
|
|
105 voiceString = ( string ) enumerator.Current;
|
106 voiceString = voiceString.Replace( "_", "underscore" );
|
|
|
108 actionsCombo.Items.Add( enumerator.Current );
|
|
|
|
|
111 speaker.Commands.Add( ( string ) enumerator.Current,
|
112 enumerator.Current, voiceString, true, false );
|
|
|
|
|
|
|
116 speaker.Commands.Add( "MoveToMouse", "MoveToMouse",
|
117 "MoveToMouse", true, true );
|
|
|
|
|
|
|
|
|
122 private void actionsCombo_SelectedIndexChanged(
|
123 object sender, EventArgs e )
|
|
|
125 speaker.StopAll( "Play" );
|
126 speaker.Play( actionsCombo.Text );
|
127 speaker.Play( "RestPose" );
|
|
|
|
|
|
|
131 private void mainAgent_Command(
|
132 object sender, AxAgentObjects._AgentEvents_CommandEvent e )
|
|
|
|
|
135 AgentObjects.IAgentCtlUserInput command =
|
136 ( AgentObjects.IAgentCtlUserInput ) e.userInput;
|
|
|
|
|
139 if ( command.Voice == "Peedy" || command.Voice == "Robby" ||
|
140 command.Voice == "Merlin" || command.Voice == "Genie" )
|
|
|
142 ChangeCharacter( command.Voice );
|
|
|
|
|
|
|
|
|
147 if ( command.Voice == "MoveToMouse" )
|
|
|
149 speaker.MoveTo( Convert.ToInt16( Cursor.Position.X - 60 ),
|
150 Convert.ToInt16( Cursor.Position.Y - 60 ), 5 );
|
|
|
|
|
|
|
|
|
155 speaker.StopAll( "Play" );
|
156 speaker.Play( command.Name );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The example also allows you to switch to a new character by speaking its name and creates a custom command,
MoveToMouse. In addition, when you press the
Speak Button, the characters speak any text that you typed in the
TextBox.
To use the Microsoft Agent control, you must add it to the
Toolbox. Select
Tools > Choose Toolbox Items... to display the
Choose Toolbox Items dialog. In the dialog, select the
COM Components tab, then scroll down and select the
Microsoft Agent Control 2.0 option. When this option is selected properly, a small check mark appears in the box to the left of the option. Click
OK to dismiss the dialog. The icon for the Microsoft Agent control now appears at the bottom of the
Toolbox. Drag the
Microsoft Agent Control 2.0 control onto your
Form and name the object
mainAgent.
In addition to the Microsoft Agent object
mainAgent (of type
AxAgent) that manages the characters, you also need a variable of type
IAgentCtlCharacter to represent the current character. We create this variable, named
speaker, in line 11.
When you execute this program, class
Agent's constructor (lines 14-42) loads the character descriptions for the predefined animated characters (lines 22-29). If the specified location of the characters is incorrect, or if any character is missing, a
FileNotFoundException is thrown. By default, the character descriptions are stored in
C:\Windows\msagent\chars. If your system uses another name for the
Windows directory, you'll need to modify the paths in lines 22-29.
Lines 32-34 set Genie as the default character, obtain all animation names via our utility method
GetAnimationNames and call
IAgentCtlCharacter method
Show to display the character. We access characters through property
Characters of
mainAgent, which contains all characters that have been loaded. We use the indexer of the
Characters property to specify the name of the character that we wish to load (Genie).
Page 1 | 2 | 3 | 4 | 5 | 6 | 7
Tutorial Index