📄 ch17.htm
字号:
<td WIDTH="99"><tt><font FACE="Courier">Left</font></tt> </td>
<td WIDTH="94">1680</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">TabIndex</font></tt> </td>
<td WIDTH="94">25</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Top</font></tt> </td>
<td WIDTH="94">3120</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Width</font></tt> </td>
<td WIDTH="94">1215</td>
</tr>
<tr>
<td WIDTH="176"><tt><font FACE="Courier">VB.CommandButton</font></tt> </td>
<td WIDTH="99"><tt><font FACE="Courier">Name</font></tt></td>
<td WIDTH="94">cmdAudio</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Caption</font></tt> </td>
<td WIDTH="94">"Pause"</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Height</font></tt> </td>
<td WIDTH="94">375</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Index</font></tt> </td>
<td WIDTH="94">2</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Left</font></tt> </td>
<td WIDTH="94">1680</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">TabIndex</font></tt> </td>
<td WIDTH="94">24</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Top</font></tt> </td>
<td WIDTH="94">2640</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Width</font></tt> </td>
<td WIDTH="94">1215</td>
</tr>
<tr>
<td WIDTH="176"><tt><font FACE="Courier">VB.CommandButton</font></tt> </td>
<td WIDTH="99"><tt><font FACE="Courier">Name</font></tt></td>
<td WIDTH="94">cmdAudio</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Caption</font></tt> </td>
<td WIDTH="94">"Forward"</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Height</font></tt> </td>
<td WIDTH="94">375</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Index</font></tt> </td>
<td WIDTH="94">1</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Left</font></tt> </td>
<td WIDTH="94">1680</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">TabIndex</font></tt> </td>
<td WIDTH="94">23</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Top</font></tt> </td>
<td WIDTH="94">2160</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Width</font></tt> </td>
<td WIDTH="94">1215</td>
</tr>
<tr>
<td WIDTH="176"><tt><font FACE="Courier">VB.CommandButton</font></tt> </td>
<td WIDTH="99"><tt><font FACE="Courier">Name</font></tt></td>
<td WIDTH="94">cmdAudio</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Caption</font></tt> </td>
<td WIDTH="94">"Rewind"</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Height</font></tt> </td>
<td WIDTH="94">375</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Index</font></tt> </td>
<td WIDTH="94">0</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Left</font></tt> </td>
<td WIDTH="94">1680</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">TabIndex</font></tt> </td>
<td WIDTH="94">22</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Top</font></tt> </td>
<td WIDTH="94">1680</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="99"><tt><font FACE="Courier">Width</font></tt> </td>
<td WIDTH="94">1215</td>
</tr>
</table>
</center></div>
<p>After adding the command button array and the frame control to the form, add the code
shown in Listing 17.6 behind the <tt><font FACE="Courier">cmdAudio_Click</font></tt>
event. </p>
<hr>
<blockquote>
<b><p>Listing 17.6. Adding code behind the <tt><font FACE="Courier">cmdAudio_Click</font></tt>
event.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Private Sub cmdAudio_Click(Index As Integer) <br>
'<br>
' handle audio playback buttons<br>
'<br>
On Error GoTo cmdAudioErr<br>
'<br>
Select Case Index<br>
Case 0 ' rewind <br>
objVText.AudioRewind
<br>
Case 1 ' forward <br>
objVText.AudioFastForward
<br>
Case 2 ' pause <br>
objVText.AudioPause
<br>
Case 3 ' resume <br>
objVText.AudioResume
<br>
Case 4 ' stop <br>
objVText.StopSpeaking
<br>
End Select<br>
'<br>
Exit Sub<br>
'<br>
cmdAudioErr:<br>
MsgBox Error$, vbCritical, "Audio Playback Error ["
& CStr(Err) & "]"<br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<h3><a NAME="GettingTTSStatusReportswiththeIsSp">Getting TTS Status Reports with the </a><tt><font
SIZE="4" FACE="Courier">IsSpeaking</font></tt><font SIZE="4"> Property</font></h3>
<p>You can use the <tt><font FACE="Courier">IsSpeaking</font></tt> property to check the
status of the TTS engine. The <tt><font FACE="Courier">IsSpeaking</font></tt> property
returns <tt><font FACE="Courier">TRUE</font></tt> if the engine is currently speaking a
message, <tt><font FACE="Courier">FALSE</font></tt> when the engine is idle. You must
check the property on a regular basis in order to get up-to-date status reports. The best
way to do this is in a timed loop. In Visual Basic, this can be done using the timer
control. </p>
<p>Add three new controls to the form: a new frame control (<tt><font FACE="Courier">Name</font></tt>=<tt><font
FACE="Courier">fraIsSp</font></tt>, <tt><font FACE="Courier">Caption</font></tt>=<tt><font
FACE="Courier">Is Speaking</font></tt>), a new label control (<tt><font FACE="Courier">Name</font></tt>=<tt><font
FACE="Courier">lblIsSpeaking</font></tt>, <tt><font FACE="Courier">Caption</font></tt>=<tt><font
FACE="Courier">False</font></tt>), and a timer control. Refer to Figure 17.6 for the
position and size of the controls. </p>
<p><a HREF="f17-6.gif"><b>Figure 17.6 : </b><i>Placing the IsSpeaking controls on the
form.</i></a> </p>
<p>After you place the controls on the form, add the code shown in Listing 17.7 to the <tt><font
FACE="Courier">Timer1_Timer</font></tt> event and the <tt><font FACE="Courier">cmdEnabled_Click</font></tt>
event. </p>
<hr>
<blockquote>
<b><p>Listing 17.7. Adding code to check the <tt><font FACE="Courier">IsSpeaking</font></tt>
property.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Private Sub Timer1_Timer()<br>
'<br>
' update label display<br>
lblIsSpeaking = CStr(objVText.IsSpeaking) <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>
' add code to get voice speed<br>
cmdSpGet_Click<br>
'<br>
' start timer to check IsSpeaking<br>
Timer1.Interval = 500<br>
Timer1.Enabled = True<br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<p>After saving and running the program, you can register and enable the TTS services and
enter text to be spoken. When you press the <tt><font FACE="Courier">Speak</font></tt>
button, you can watch the <tt><font FACE="Courier">Is Speaking</font></tt> label change
from <tt><font FACE="Courier">False</font></tt> to <tt><font FACE="Courier">True</font></tt>
and back again to <tt><font FACE="Courier">False</font></tt>. </p>
<h3><a NAME="EstablishingaTTSCallbackinVisualBa">Establishing a TTS Callback in Visual
Basic 4.0</a></h3>
<p>If you are using Visual Basic 4.0 as the platform for the OLE Voice Text library, you
can use the <tt><font FACE="Courier">Callback</font></tt> property to establish an
asynchronous callback from the TTS engine to your Visual Basic program. This link is
established by adding a class module to your Visual Basic program. This class module must
contain two methods: <tt><font FACE="Courier">SpeakingStarted</font></tt> and <tt><font
FACE="Courier">SpeakingDone</font></tt>. These methods will be automatically invoked by
the TTS engine at the appropriate time. </p>
<p>To establish a TTS callback in Visual Basic 4.0, first add a class module. Set its <tt><font
FACE="Courier">Name</font></tt> property to <tt><font FACE="Courier">clsCallBack</font></tt>,
its <tt><font FACE="Courier">Instance</font></tt> property to <tt><font FACE="Courier">MultiUse,
Creatable</font></tt>, and its <tt><font FACE="Courier">Public</font></tt> property to <tt><font
FACE="Courier">TRUE</font></tt>. Then add the code in Listing 17.8. </p>
<hr>
<blockquote>
<b><p>Listing 17.8. Adding methods to the <tt><font FACE="Courier">clsCallBack</font></tt>
class module.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Public Function SpeakingDone()<br>
'<br>
' this method will execute when the<br>
' TTS engine stops speaking text<br>
'<br>
frmVText.lblCallBack = "TTS Engine Idle"<br>
'<br>
End Function<br>
<br>
Public Function SpeakingStarted()<br>
'<br>
' this method will execute when the<br>
' TTS engine starts speaking text<br>
'<br>
frmVText.lblCallBack = "TTS Engine Active"<br>
'<br>
End Function</font></tt> </p>
</blockquote>
<hr>
<p>The code in Listing 17.8 creates the class and methods required to link the TTS engine
with your Visual Basic application. The next step is to initialize the <tt><font
FACE="Courier">Callback</font></tt> property. In this step, you are telling the TTS engine
the name of the project and the name of the class module that has the two required
methods. </p>
<p>To do this, add another command button, frame control, and label control to the form.
Use Figure 17.7 and Table 17.6 to place and size the controls. </p>
<p><a HREF="f17-7.gif"><b>Figure 17.7 :</b> <i>Placement of the Callback controls on the
form.</i></a> <br>
</p>
<p align="center"><b>Table 17.6. The <tt><font FACE="Courier">Callback</font></tt>
controls.</b> </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><i>Control</i></td>
<td WIDTH="109"><i>Property</i> </td>
<td WIDTH="109"><i>Setting</i></td>
</tr>
<tr>
<td WIDTH="176"><tt><font FACE="Courier">VB.CommandButton</font></tt> </td>
<td WIDTH="109"><tt><font FACE="Courier">Name</font></tt></td>
<td WIDTH="109">cmdCallBack</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="109"><tt><font FACE="Courier">Caption</font></tt> </td>
<td WIDTH="109">"&CallBack"</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="109"><tt><font FACE="Courier">Height</font></tt> </td>
<td WIDTH="109">495</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="109"><tt><font FACE="Courier">Left</font></tt> </td>
<td WIDTH="109">240</td>
</tr>
<tr>
<td WIDTH="176"> </td>
<td WIDTH="109"><tt><font FACE="Courier">TabIndex</font></tt> </td>
<td WIDTH="109">32</td>
</tr>
<tr>
<td WIDTH="17
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -