Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
C# for Programmers, 2/e

© 2006
pages: ~1350


This tutorial introduces Microsoft Agent—a 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)
Responding to the Agent Control's ClickEvent
When a user clicks the character (i.e., pokes it with the mouse), event handler mainAgent_ClickEvent (lines 58-64) executes. First, speaker method Play plays an animation. This method accepts as an argument a string representing one of the predefined animations for the character (a list of animations for each character is available at the Microsoft Agent Web site; each character provides over 70 animations). In our example, the argument to Play is "Confused"-this animation is defined for all four characters, each of which expresses this emotion in a unique way. The character then speaks, "Why are you poking me?" via a call to method Speak. Finally, we play the RestPose animation, which returns the character to its neutral, resting pose.
Obtaining a Character's List of Animations and Defining Its Commands
The list of valid commands for a character is contained in property Commands of the IAgentCtlCharacter object (speaker, in this example). The commands for an Agent character can be viewed in the Commands pop-up window, which displays when the user right-clicks an Agent character (the last screenshot in Fig. 17.37). Method Add of property Commands adds a new command to the command list. Method Add takes three string arguments and two bool arguments. The first string argument identifies the name of the command, which we use to identify the command programmatically. The second string defines the command name as it appears in the Commands pop-up window. The third string defines the voice input that triggers the command. The first bool specifies whether the command is active, and the second bool indicates whether the command is visible in the Commands pop-up window. A command is triggered when the user selects the command from the Commands pop-up window or speaks the voice input into a microphone. Command logic is handled in the Command event handler of the AxAgent control (mainAgent, in this example). In addition, Agent defines several global commands that have predefined functions (for example, speaking a character name causes that character to appear).
Method GetAnimationNames (lines 86-119) fills the actionsCombo ComboBox with the current character's animation listing and defines the valid commands that can be used with the character. The method contains a lock block to prevent errors resulting from rapid character changes. The method uses an IEnumerator (lines 92-93) to obtain the current character's animations. Lines 98-99 clear the existing items in the ComboBox and the character's Commands property. Lines 102-113 iterate through all items in the animation-name enumerator. For each animation, line 105 assigns the animation name to string voiceString. Line 106 removes any underscore characters (_) and replaces them with the string "underscore"; this changes the string so that a user can pronounce and employ it as a command activator. Line 108 adds the animation's name to the actionsCombo ComboBox. The Add method of the Commands property (lines 111-112) adds a new command to the current character. In this example, we add every animation name as a command. Each call to Add receives the animation name as both the name of the command and the string that appears in the Commands pop-up window. The third argument is the voice command, and the last two arguments enable the command but indicate that it is not available via the Commands pop-up window. Thus, the command can be activated only by voice input. Lines 116-117 create a new command, named MoveToMouse, which is visible in the Commands pop-up window.

Page 1 | 2 | 3 | 4 | 5 | 6 | 7

Tutorial Index