Rss & SiteMap
Foxtable(狐表) http://www.foxtable.com
语音朗读
FoxTable可以自动将一段文本朗读出来。
在默认的情况下,Windows
Vista/7可以正常朗读中英文,而Windows XP只能朗读英文,不过你可以另外安装语音库来解决这个问题。
实际上,使你用的是Windows Vista/7,也可以另外安装语音库来获得更好的朗读效果。
提示:无需去刻意理解下面的代码,实际工作的时候,只需套用即可。
示例一
如果你要用默认的设置朗读语音,那么很简单:
Dim sp As New
DotNetSpeech.SpVoice()
sp.Speak("I am from
china.",
DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync)
执行上面的代码,计算机或自动朗读出“I am from china.”
示例二
上面的朗读代码是异步的,在朗读的同时,你可以继续进行下一步的操作。
如果是同步朗读,那么在朗读过程中,将挂起程序,不能进行任何操作,直到朗读结束。
同步朗读的代码:
Dim sp As New
DotNetSpeech.SpVoice()
sp.Speak("I am from
china.",
DotNetSpeech.SpeechVoiceSpeakFlags.SVSFDefault)
示例三
通过设置Rate属性,可以调整朗读语速,Rate的默认值是0,可以为负数,值越大,语速越快。
你可以在命令窗口分别执行下面两段代码,比较一下朗读速度。
快速朗读:
Dim sp As New
DotNetSpeech.SpVoice()
sp.rate = 3
sp.Speak("I am
from china.",
DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync)
低速朗读:
Dim sp As New
DotNetSpeech.SpVoice()
sp.rate = -3
sp.Speak("I am from
china.",
DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync)
示例四
你可以通过下面的代码来获得所有已经安装的语音库的名称:
Dim sp As new
DotNetSpeech.SpVoice()
For Each
st As
Dotnetspeech.SpObjectToken In sp.GetVoices
Output.Show(st.GetDescription)
Next
示例五
朗读的时候,可以选择语音库,例如选择1号语音库朗读:
Dim sp As New
DotNetSpeech.SpVoice()
sp.Voice = sp.GetVoices(String.Empty,String.Empty).Item(0)
sp.Speak("I am from
china.",
DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync)
示例六
可以将朗读结果录制为一个wav文件,例如:
Dim Voice As
new
DotNetSpeech.SpVoice()
Dim Stream As new
DotNetSpeech.SpFileStreamClass()
Stream.Open("c:\temp\test.wav",
DotNetSpeech.SpeechStreamFileMode.SSFMCreateForWrite, False)
Voice.AudioOutputStream
= Stream
Voice.Speak("I love you.",DotNetSpeech.SpeechVoiceSpeakFlags.SVSFDefault)
Stream.Close()
示例七
这是一个综合示例,可以打开CaseStudy目录下的示例文件:语音朗读.Table
本示例的界面如下:
该示例可以列出已经安装的语音库,客户用已选择语音库,设置语速,并可选择是异步还是同步朗读。
窗口的AfterLoad事件代码为:
Dim sp As new
DotNetSpeech.SpVoice()
Dim ls As WinForm.ListBox = e.Form.Controls("ListBox1")
For
Each st As Dotnetspeech.SpObjectToken
In
sp.GetVoices
ls.Items.Add(st.GetDescription)
Next
If ls.items.count
> 0
Then
ls.SelectedIndex = 0
End
If
上面的代码会在窗口的ListBox1中列出已经安装的语音库的名称,并默认选择第一个语音库。
开始朗读按钮的代码为:
Dim yy As WinForm.ListBox =
e.Form.Controls("ListBox1")
Dim sp
As New
DotNetSpeech.SpVoice()
Dim vl As String = e.Form.Controls("txtContent").Value
'获得要朗读的文本
sp.Rate =
e.Form.Controls("boxSpeed").Value
'设置语速
sp.Voice = sp.GetVoices(String.Empty,String.Empty).Item(yy.SelectedIndex) '选择语音库
If e.Form.Controls("rdoAsync").Checked
Then
sp.Speak(vl,
DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync) '异步朗读
Else
sp.Speak(vl, DotNetSpeech.SpeechVoiceSpeakFlags.SVSFDefault)
'同步朗读
End
If
Dim sp As New DotNetSpeech.SpVoice()
sp.Speak("I am from china.", DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync)
怎么把"I am from china."换成单元格内容?
在原示例窗口中增加“语音校对”按钮
Dim yy As WinForm.ListBox = e.Form.Controls("ListBox1")
Dim sp As New DotNetSpeech.SpVoice()
Dim vl As String = e.Form.Controls("txtContent").Value '获得要朗读的文本
sp.Rate = e.Form.Controls("boxSpeed").Value '设置语速
sp.Voice = sp.GetVoices(String.Empty,String.Empty).Item(yy.SelectedIndex) '选择语音库
With CurrentTable
For r As Integer = .TopPosition To .BottomPosition
For c As Integer = .LeftCol To .RightCol
Dim v As String = .Rows(r)(c)
If e.Form.Controls("rdoAsync").Checked Then
sp.Speak(v, DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync) '异步朗读
Else
sp.Speak(v, DotNetSpeech.SpeechVoiceSpeakFlags.SVSFDefault) '同步朗读
End If
Next
Next
End With