📄 ch17.htm
字号:
<html>
<head>
<title>Chapter 17 -- SAPI Tools-Using SAPI Objects with Visual Basic 4.0</title>
<meta NAME="GENERATOR" CONTENT="Microsoft FrontPage 3.0">
</head>
<body TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<!-- Spidersoft WebZIP Ad Banner Insert -->
<!-- End of Spidersoft WebZIP Ad Banner Insert-->
<h1><font COLOR="#FF0000">Chapter 17</font></h1>
<h1><b><font SIZE="5" COLOR="#FF0000">SAPI Tools-Using SAPI Objects with Visual Basic 4.0</font></b>
</h1>
<hr WIDTH="100%">
<h3 ALIGN="CENTER"><font SIZE="+2" COLOR="#000000">CONTENTS<a NAME="CONTENTS"></a> </font></h3>
<ul>
<li><a HREF="#OLEVoiceTextObject">OLE Voice Text Object</a> <ul>
<li><a HREF="#UsingtheVisualBasicObjectBrowser">Using the Visual Basic Object Browser</a> </li>
<li><a HREF="#UsingtheRegisterMethodtoConnecttotheSR">Using the Register Method to Connect
to the TTS Engine</a> </li>
<li><a HREF="#UsingtheEnablePropertytoStartandS">Using the Enable Property to Start and Stop
the TTS Engine</a> </li>
<li><a HREF="#UsingtheSpeakMethodtoPlayText">Using the Speak Method to Play Text</a> </li>
<li><a HREF="#AdjustingtheSpeedofVoicePlayback">Adjusting the Speed of Voice Playback</a> </li>
<li><a HREF="#AddingPlaybackControlsforTTSService">Adding Playback Controls for TTS Services</a>
</li>
<li><a HREF="#GettingTTSStatusReportswiththeIsSp">Getting TTS Status Reports with the
IsSpeaking Property</a> </li>
<li><a HREF="#EstablishingaTTSCallbackinVisualBa">Establishing a TTS Callback in Visual
Basic 4.0</a> </li>
</ul>
</li>
<li><a HREF="#OLEVoiceCommandObjects">OLE Voice Command Objects</a> <ul>
<li><a HREF="#CreatingtheVoiceCommandMenuObject">Creating the Voice Command Menu Object</a> </li>
<li><a HREF="#UsintheRegisterMethodtoConnecttot">Using the Register Method to Connect to the
SR Engine</a> </li>
<li><a HREF="#UsingtheAwakePropertytoStartandSt">Using the Awake Property to Start and Stop
SR Processing</a> </li>
<li><a HREF="#CreatingtheMenuObject">Creating the Menu Object</a> </li>
<li><a HREF="#AddingCommandstotheVoiceMenuObject">Adding Commands to the Voice Menu Object</a>
</li>
<li><a HREF="#UsingtheCommandSpokenPropertytoResp">Using the CommandSpoken Property to
Respond to Menu Commands</a> </li>
<li><a HREF="#EstablishinganSRCallbackinVisualBa">Establishing an SR Callback in Visual
Basic 4.0</a> </li>
<li><a HREF="#CreatingListCommandsfortheVoiceMen">Creating List Commands for the Voice Menu
Object</a> </li>
<li><a HREF="#RemovingCommandsfromtheVoiceMenuOb">Removing Commands from the Voice Menu
Object</a> </li>
</ul>
</li>
<li><a HREF="#Summary">Summary</a> </li>
</ul>
<hr>
<p>The Microsoft Speech SDK contains a set of OLE library files for implementing SAPI
services using Visual Basic and other VBA-compatible languages. In this chapter, you'll
learn how to use the objects, methods, and properties in the OLE library to add speech
recognition (SR) and text-to-speech (TTS) services to your Windows applications. </p>
<p>There are two OLE libraries:
<ul>
<li><font COLOR="#000000">The OLE Voice Text library (</font><tt><font FACE="Courier">VTXTAUTO.TLB</font></tt>)
</li>
<li><font COLOR="#000000">The OLE Voice Command library (</font><tt><font FACE="Courier">VCMDAUTO.TLB</font></tt>)
</li>
</ul>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>The Microsoft SDK supplies these files in the <tt><font FACE="Courier">REDIST</font></tt>
folder that is created when you install the SDK. You should copy these files from the <tt><font
FACE="Courier">REDIST</font></tt> folder into the <tt><font FACE="Courier">WINDOWS\SYSTEM</font></tt>
folder before you begin the examples in this book. </p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>You will first learn to use the <tt><font FACE="Courier">Voice Text</font></tt> object
to add TTS services to Visual Basic applications. Once you know how to use the <tt><font
FACE="Courier">Voice Text</font></tt> object, you'll learn how to use the <tt><font
FACE="Courier">Voice Command</font></tt> and <tt><font FACE="Courier">Voice Menu</font></tt>
objects to add SR services to Visual Basic applications. </p>
<p>If you have not yet done so, start Visual Basic 4.0 and begin a new project. You'll use
Visual Basic to complete all the examples in this section. </p>
<h2><a NAME="OLEVoiceTextObject"><font SIZE="5" COLOR="#FF0000">OLE </font><tt><font
SIZE="5" COLOR="#FF0000" FACE="Courier">Voice Text</font></tt><font SIZE="5"
COLOR="#FF0000"> Object</font></a></h2>
<p>The <tt><font FACE="Courier">Voice Text</font></tt> object is used to provide access to
the text-to-speech services of the installed TTS engine. Using the <tt><font
FACE="Courier">Voice Text</font></tt> object involves only a few simple steps. First, you
must register your application with the SAPI services on the workstation. Then you can
send text to the <tt><font FACE="Courier">Voice Text</font></tt> object for playback. You
can use several other methods to rewind, fast-forward, pause, and restart audio playback
of the text message. </p>
<h3><a NAME="UsingtheVisualBasicObjectBrowser">Using the Visual Basic Object Browser</a></h3>
<p>When using Visual Basic, you can use the <tt><font FACE="Courier">Tools | References</font></tt>
menu option to load the object library into the Visual Basic design-time environment (see
Figure 17.1). </p>
<p><a HREF="f17-1.gif"><b>Figure 17.1 : </b><i>Loading the OLE Voice Text library into
Visual Basic at design time.</i></a> </p>
<p>The advantage of this method is that you can use the Visual Basic Object browser to
view all the objects, methods, and properties of the library at design time (see Figure
17.2). </p>
<p><a HREF="f17-2.gif"><b>Figure 17.2 : </b><i>Using the Visual Basic Object Browser to
inspect the Voice Text library.</i></a> </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>Other VBA-compliant languages (Excel, Word, Access, and so on) cannot pre-load the
object library. You can still use the SAPI OLE objects as described here, but you will not
be able to use the object browser to view the object details at run-time.</p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>Start a new Visual Basic project and add the following code to the general declarations
section: </p>
<p><tt><font FACE="Courier">Dim objVText as Object ' declare TTS object variable</font></tt>
</p>
<p>You'll use this variable throughout the project. This is the top-level object for all
TTS services. Next you need to add code to the <tt><font FACE="Courier">Form_Load</font></tt>
event to initialize the TTS object. Also, you need to add code to the <tt><font
FACE="Courier">Form_Unload</font></tt> event to destroy the TTS object upon exit. Add the
code shown in Listing 17.1 to your project. </p>
<hr>
<blockquote>
<b><p>Listing 17.1. Adding code to the <tt><font FACE="Courier">Form_Load</font></tt> and <tt><font
FACE="Courier">Form_Unload</font></tt> events.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Private Sub Form_Load()<br>
'<br>
' create voice text object<br>
'<br>
Set objVText = CreateObject("Speech.VoiceText") <br>
'<br>
End Sub<br>
<br>
Private Sub Form_Unload(Cancel As Integer)<br>
'<br>
' destroy voice object<br>
'<br>
Set objVText = Nothing<br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Tip</b></td>
</tr>
<tr>
<td><blockquote>
<p>It's always a good idea to destroy the object variable once you no longer need it.
Setting the object equal to <tt><font FACE="Courier">Nothing</font></tt> clears memory of
any remaining references to the unused objects. </p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<h3><a NAME="UsingtheRegisterMethodtoConnecttotheSR">Using the <tt><font SIZE="4"
FACE="Courier">Register</font></tt><font SIZE="4"> Method to Connect to the TTS Engine</font></a></h3>
<p>Once you create a valid TTS object, you must use the <tt><font FACE="Courier">Register</font></tt>
method to register your application with the TTS engine. There are two parameters that you
can use with the <tt><font FACE="Courier">Register</font></tt> method. The first parameter
allows you to select the output device that the TTS engine will use for playback. You can
leave this parameter empty in order to use the default device. You can also enter the name
of any other valid line device such as an attached telephone handset. The second parameter
is the name of the application that is requesting TTS services. This second parameter
cannot be left blank. </p>
<p>To test the <tt><font FACE="Courier">Register</font></tt> method, add a new command
button to the project, and set its <tt><font FACE="Courier">Name</font></tt> property to <tt><font
FACE="Courier">cmdReg</font></tt> and its caption to <tt><font FACE="Courier">&Register</font></tt>.
Then add the code shown in Listing 17.2 to the <tt><font FACE="Courier">CmdReg_Click</font></tt>
event. </p>
<hr>
<blockquote>
<b><p>Listing 17.2. Adding code to the <tt><font FACE="Courier">cmdReg_Click</font></tt>
event.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Private Sub cmdReg_Click()<br>
'<br>
' register the app w/ TTS engine<br>
'<br>
Dim cDevice As String<br>
Dim cAppName As String<br>
'<br>
cDevice = "" ' use default value <br>
cAppName = "Voice Text Demo" <br>
'<br>
objVText.register cDevice, cAppName<br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<p>Now save the form as <tt><font FACE="Courier">VTEXT.FRM</font></tt> and the project as <tt><font
FACE="Courier">VTEXT.VBP</font></tt>, and run the project. When you press the <tt><font
FACE="Courier">Register</font></tt> button, you will register your application as ready
for TTS services. </p>
<h3><a NAME="UsingtheEnablePropertytoStartandS">Using the <tt><font SIZE="4"
FACE="Courier">Enable</font></tt><font SIZE="4"> Property to Start and Stop the TTS Engine</font></a></h3>
<p>Once you have registered your application with the TTS engine, you must set the <tt><font
FACE="Courier">Enabled</font></tt> property to <tt><font FACE="Courier">TRUE</font></tt>
before you can actually send text to the engine for output. </p>
<p>Add two new buttons to the form. Set the <tt><font FACE="Courier">Name</font></tt>
property and <tt><font FACE="Courier">Caption</font></tt> property of one button to <tt><font
FACE="Courier">cmdEnable</font></tt> and <tt><font FACE="Courier">&Enable</font></tt>,
respectively. Set the <tt><font FACE="Courier">Name</font></tt> and <tt><font
FACE="Courier">Caption</font></tt> properties of the next button to <tt><font
FACE="Courier">cmdDisable</font></tt> and <tt><font FACE="Courier">&Disable</font></tt>.
Then add the code shown in Listing 17.3. </p>
<hr>
<blockquote>
<b><p>Listing 17.3. Adding code to the <tt><font FACE="Courier">cmdEnable</font></tt> and <tt><font
FACE="Courier">cmdDisable</font></tt> buttons.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Private Sub cmdDisable_Click()<br>
'<br>
' disabling the engine for this site<br>
'<br>
objVText.Enabled = False<br>
'<br>
End Sub<br>
<br>
Private Sub cmdEnable_Click()<br>
'<br>
' enable the engine for this site<br>
'<br>
objVText.Enabled = True<br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td WIDTH="579"><blockquote>
<p>The <tt><font FACE="Courier">Enabled</font></tt> property affects all applications
using TTS services at your workstation. <i>Use this setting with caution.</i> Changing the
<tt><font FACE="Courier">Enabled</font></tt> property to <tt><font FACE="Courier">FALSE</font></tt>
will disable all TTS services registered on your pc. For this reason, it is a good idea to
check the <tt><font FACE="Courier">Enabled</font></tt> property each time you attempt to
speak text. </p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<h3><a NAME="UsingtheSpeakMethodtoPlayText">Using the <tt><font SIZE="4" FACE="Courier">Speak</font></tt><font
SIZE="4"> Method to Play Text</font></a></h3>
<p>Once you have registered your application with the TTS engine and set the <tt><font
FACE="Courier">Enabled</font></tt> property to <tt><font FACE="Courier">TRUE</font></tt>,
you can send text to the engine for playback. The <tt><font FACE="Courier">Speak</font></tt>
method takes two parameters. The first parameter is the text to play back, and the second
parameter is a flag parameter that can be used to establish the statement type and queue
priority of the text being sent to the TTS engine. </p>
<p>Every text message sent to the TTS engine must be declared as one of seven types. The
OLE Voice Text library has a set of pre-defined constants that can be used to set the
statement type of the <tt><font FACE="Courier">Speak</font></tt> method option flag. These
types are shown in Table 17.1.<br>
</p>
<p align="center"><b>Table 17.1. Statement types used by the SAPI TTS engine.</b> </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><i>Pre-Defined Constant</i></td>
<td WIDTH="60"><i>Value</i> </td>
<td WIDTH="335"><i>Description</i></td>
</tr>
<tr>
<td WIDTH="195"><tt><font FACE="Courier">Vtxtst_STATEMENT</font></tt> </td>
<td WIDTH="60">1</td>
<td WIDTH="335">A neutral informative statement, such as You have new messages. This is
the default type of speech. </td>
</tr>
<tr>
<td WIDTH="195"><tt><font FACE="Courier">Vtxtst_QUESTION</font></tt> </td>
<td WIDTH="60">2</td>
<td WIDTH="335">A question, such as Do you want to save the file? </td>
</tr>
<tr>
<td WIDTH="195"><tt><font FACE="Courier">Vtxtst_COMMAND</font></tt> </td>
<td WIDTH="60">4</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -