安装SDK后,运行VB,新建一工程,然后点击菜单“工程”—“引用”,在列表中把Microsoft Speech Object Library引用工程,放入一些控件。下表列出用到的控件及它们的属性 控件名 属性 属性值 text1 text “” command1,2 caption 朗读,退出 command3(控件数据) caption +、-、+、- list1(列表框,用来放声音种类) label1,2 caption 速度,音量 运行界面如下图 源程序如下: Option Explicit Dim WithEvents Vo As SpeechLib.SpVoice Dim vt As SpeechLib.SpObjectToken ''退出程序 Private Sub Command2_Click() Unload Me End Sub ''根据控件组增加声速或音量, 声速范围-10-10,音量范围0-100 Private Sub Command3_Click(Index As Integer) Select Case Index Case 0 If Vo.Rate < 10 Then Vo.Rate = Vo.Rate + 1 Call lblchange End If Case 1 If Vo.Rate > -10 Then Vo.Rate = Vo.Rate - 1 Call lblchange End If Case 2 If Vo.Volume < 100 Then Vo.Volume = Vo.Volume + 1 Call lblchange End If Case 3 If Vo.Volume > 0 Then Vo.Volume = Vo.Volume - 1 Call lblchange End If End Select End Sub Private Sub Form_Load() Set Vo = New SpeechLib.SpVoice Label1.Caption = "速度:" & Vo.Rate Label2.Caption = "音量:" & Vo.Volume ''文本框放入的是要朗读的文章 Text1.Text = "In order to understand voice events, it is necessary to distinguish between” _ & “the TTS engine, which synthesizes speech from text, and the SpVoice object,”_ & ” which applications employ to communicate with the engine. The TTS engine is “ _ & “somewhat like a server, and the " Call ShowVoice End Sub Private Sub Command1_Click() On Error GoTo Error ''根据列表框的选项选择声音,默认为Microsoft Mary. ''也可以通过GetVoices方法,设置声音为男性或女性等 ''Set Vo.Voice = Vo.GetVoices("gender=female").Item(0) 声音为女性 If List1.ListIndex > -1 Then Set Vo.Voice = Vo.GetVoices.Item(List1.ListIndex) End If Debug.Print Vo.Voice.GetDescription Select Case Command1.Caption Case "朗读" If Not Text1.Text = "" Then ''SVSFlagsAsync 为阅读标记,默认为同步,本例为异步 Vo.Speak Text1.Text, SVSFlagsAsync End If Text1.SetFocus Command1.Caption = "暂停" Case "暂停" Vo.Pause Command1.Caption = "继续" Case "继续" Vo.Resume Command1.Caption = "暂停" End Select Exit Sub Error: MsgBox "对不起,朗读出错了", vbOKOnly End Sub ''朗读完毕,把Command1.caption设为朗读 Private Sub Vo_EndStream(ByVal StreamNumber As Long, ByVal StreamPosition As Variant) Command1.Caption = "朗读" End Sub Private Sub lblchange() Label1.Caption = "速度:" & Vo.Rate Label2.Caption = "音量:" & Vo.Volume End Sub ''ShowVoice函数取得声音集合,放入列表框 Private Sub ShowVoice() For Each vt In Vo.GetVoices List1.AddItem vt.GetDescription Next End Sub ''朗读一个Word(单词),产生一次Word事件,参数StreamPosition为输出声音流中该单词的位置,CharacterPosition为该单词在文本中的位置,Length为单词长度 ''通过Word事件,可以直观的看到读到的单词 Private Sub Vo_Word(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, ByVal CharacterPosition As Long, ByVal Length As Long) Text1.SelStart = CharacterPosition Text1.SelLength = Length End Sub 以上程序在VB6+Win98下通过。 ------------------------------------------------------------------------------------------------------------ SpVoice对象的属性和方法 属性: AlertBoundary 分隔类型,该属性不同,产生的事件就不同 AllowAudioOutputFormatChangesOnNextSet 允许用户是否能调整音频格式 AudioOutputStream 通过该属性获得由对象SpFileStream打开的文件数据流,然后通过speak方法朗读出来 EventInterests 与AlertBoundary类似,用来产生事件的类型 Priority 优先权 Rate 朗读速度 Voice 声音类型 Volume 声音大小 方法:GetVoices 到得声音类型 Speak 开始朗读 Pause 暂停 Resume 从暂停处开始朗读 Skip 跳过若干单词或句子 SpeakStream 朗读文件数据流 WaitUntilDone 延迟一定时间,单位为毫秒 事件: Word 每读完一个单词便产生一次该事件 VoiceChange 声音改变就产生 Phoneme 每个音位产生 EndStream 朗读完毕后产生 以上的方法和事件如果与属性AlertBoundary,EventInterests结合就能编出很优秀的程序。欢迎大家与我交流(elby@21cn.com)。
|