Deitel & Associates, Inc. Logo

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

ISBN:
0-13-134591-5
© 2006
pages: ~1350

Order

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)
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.
1   // Fig. 17.28: Agent.cs
2   // Microsoft Agent demonstration.
3   using System;
4   using System.Collections;
5   using System.Windows.Forms;
6   using System.IO;
7   
8   public partial class Agent : Form
9   {
10      // current agent object
11      private AgentObjects.IAgentCtlCharacter speaker;
12   
13      // default constructor
14      public Agent()
15      {
16         InitializeComponent();
17   
18         // initialize the characters
19         try
20         {
21            // load characters into agent object
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" );
30   
31             // set current character to Genie and show him
32            speaker = mainAgent.Characters[ "Genie" ];
33            GetAnimationNames(); // obtain an animation name list
34            speaker.Show( 0 ); // display Genie
35            characterCombo.SelectedText = "Genie";
36         } // end try
37         catch ( FileNotFoundException )
38         {
39            MessageBox.Show( "Invalid character location",
40               "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
41         } // end catch
42      } // end constructor
43   
44      // event handler for Speak Button
45      private void speakButton_Click( object sender, EventArgs e )
46      {
47         // if textbox is empty, have the character ask
48         // user to type the words into the TextBox; otherwise,
49         // have the character say the words in the TextBox
50         if ( speechTextBox.Text == "" )
51            speaker.Speak(
52               "Please, type the words you want me to speak", "" );
53         else
54            speaker.Speak( speechTextBox.Text, "" );
55      } // end method speakButton_Click
56   
57      // event handler for Agent control's ClickEvent
58      private void mainAgent_ClickEvent(
59         object sender, AxAgentObjects._AgentEvents_ClickEvent e )
60      {
61         speaker.Play( "Confused" );
62         speaker.Speak( "Why are you poking me?", "" );
63         speaker.Play( "RestPose" );
64      } // end method mainAgent_ClickEvent
65   
66      // ComboBox changed event, switch active agent character
67      private void characterCombo_SelectedIndexChanged(
68         object sender, EventArgs e )
69      {
70         ChangeCharacter( characterCombo.Text );
71      } // end method characterCombo_SelectedIndexChanged
72   
73      // utility method to change characters
74      private void ChangeCharacter( string name )
75      {
76         speaker.StopAll( "Play" );
77         speaker.Hide( 0 );
78         speaker = mainAgent.Characters[ name ];
79   
80         // regenerate animation name list
81         GetAnimationNames();
82         speaker.Show( 0 );
83      } // end method ChangeCharacter
84   
85      // get animation names and store in ArrayList
86      private void GetAnimationNames()
87      {
88         // ensure thread safety
89         lock ( this )
90         {
91            // get animation names
92            IEnumerator enumerator = mainAgent.Characters[
93               speaker.Name ].AnimationNames.GetEnumerator();
94   
95            string voiceString;
96   
97            // clear actionsCombo
98            actionsCombo.Items.Clear();
99            speaker.Commands.RemoveAll();
100   
101            // copy enumeration to ArrayList
102            while ( enumerator.MoveNext() )
103            {
104               // remove underscores in speech string
105               voiceString = ( string ) enumerator.Current;
106               voiceString = voiceString.Replace( "_", "underscore" );
107   
108               actionsCombo.Items.Add( enumerator.Current );
109   
110               // add all animations as voice enabled commands
111               speaker.Commands.Add( ( string ) enumerator.Current,
112                  enumerator.Current, voiceString, true, false );
113            } // end while
114   
115            // add custom command
116            speaker.Commands.Add( "MoveToMouse", "MoveToMouse",
117                "MoveToMouse", true, true );
118         } // end lock
119      } // end method GetAnimationNames
120   
121      // user selects new action
122      private void actionsCombo_SelectedIndexChanged(
123         object sender, EventArgs e )
124      {
125         speaker.StopAll( "Play" );
126         speaker.Play( actionsCombo.Text );
127         speaker.Play( "RestPose" );
128      } // end method actionsCombo_SelectedIndexChanged
129   
130      // event handler for Agent commands
131      private void mainAgent_Command(
132         object sender, AxAgentObjects._AgentEvents_CommandEvent e )
133      {
134         // get UserInput object
135         AgentObjects.IAgentCtlUserInput command =
136            ( AgentObjects.IAgentCtlUserInput ) e.userInput;
137   
138         // change character if user speaks character name
139         if ( command.Voice == "Peedy" || command.Voice == "Robby" ||
140            command.Voice == "Merlin" || command.Voice == "Genie" )
141         {
142            ChangeCharacter( command.Voice );
143            return;
144         } // end if
145   
146         // send agent to mouse
147         if ( command.Voice == "MoveToMouse" )
148         {
149            speaker.MoveTo( Convert.ToInt16( Cursor.Position.X - 60 ),
150               Convert.ToInt16( Cursor.Position.Y - 60 ), 5 );
151            return;
152         } // end if
153   
154         // play new animation
155         speaker.StopAll( "Play" );
156         speaker.Play( command.Name );
157      }
158   } // end class Agent
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