当前位置: 移动技术网 > IT编程>脚本编程>VBScript > vbs 调用中文语音让你电脑听你的命令的实现代码

vbs 调用中文语音让你电脑听你的命令的实现代码

2017年12月08日  | 移动技术网IT编程  | 我要评论

锡,鑫龙墙画,出境自助游

如果你机器本身装了office的话,可能不需要装以下软件,我机器由于装的是openoffice,所以我提前装了
speechsdk51.exe
cnspeechpackage.msi
这两个软件。如果让电脑朗读,参考下面的代码
复制代码 代码如下:

readalltextfile=createobject("scripting.filesystemobject").opentextfile(wscript.arguments(0), 1).readall
function regexptest(patrn, strng)
dim regex, match, matches ' 建立变量。
set regex = new regexp ' 建立正则表达式。
regex.pattern = patrn ' 设置模式。
regex.ignorecase = true ' 设置是否区分大小写。
regex.global = true ' 设置全程可用性。
set matches = regex.execute(strng) ' 执行搜索。
for each match in matches ' 遍历 matches 集合。

retstr = retstr & match.value&" "
retstr = retstr & vbcrlf
next
regexptest = retstr
end function

set objvoice = createobject("sapi.spvoice")
set objvoice.voice = objvoice.getvoices("name=microsoft simplified chinese").item(0)
objvoice.rate = 3 '速度:-10,10 0
objvoice.volume = 100 '声音:0,100 100
objvoice.speak regexptest("[\u4e00-\u9fa5]", readalltextfile)

或是装完speechsdk51.exe之后可以参考
c:\program files\microsoft speech sdk 5.1\samples\scripts\simpletts\simpletts.html这个文件。
如果让电脑听你的命令,下边是vbs代码
复制代码 代码如下:

'==========================================================================
' name : commandpc.vbs
' author : huaying
' date : 2005-1-31
'==========================================================================
dim commanddictionary '命令字典对象
dim wshshell 'wshshell对象提供对本地windows程序的访问。
dim scriptcomplete '程序结束标志
dim sr '语音识别(speech recognition)对象
dim grammar '语音识别的命令语法对象
'初始化命令字典对象,可根据自己的需要添加命令
set commanddictionary = createobject("scripting.dictionary")
commanddictionary.add "上网","""c:\program files\internet explorer\iexplore.exe""" '注意双引号的数目
commanddictionary.add "计算器", "calc"
commanddictionary.add "记事本", "notepad"
commanddictionary.add "空当接龙", "freecell"
set wshshell = createobject("wscript.shell") '创建wshshell对象
scriptcomplete = false '初始化程序结束标志
'创建语音识别对象,调用由"command.xml"所定义的语法,并启动语音识别引擎
set sr = wscript.createobject("sapi.spsharedrecocontext", "recocontext_")
set grammar = sr.creategrammar
grammar.cmdloadfromfile "x.xml", slodynamic
grammar.cmdsetruleidstate 0, 1
msgbox "你好,主人,请吩咐。"
'等候你的语音命令(需要安装麦克风)
'当识别出"命令结束"命令时程序结束
do
wscript.sleep 1000
loop until scriptcomplete
msgbox "欢迎再跟我说话,再见!"
'你的语音命令被识别
sub recocontext_recognition(byval streamnumber,byval streamposition,byval recognitiontype,byval result )
text = result.phraseinfo.gettext '获取语音识别引擎所识别的命令
if text <> "命令结束" then
wshshell.run commanddictionary.item(text) '由wshshell对象run方法执行你的命令
else
scriptcomplete = true '程序结束标志
end if
end sub

==================================
x.xml源码
复制代码 代码如下:

<?xml version="1.0" encoding="gb2312" ?>
<grammar langid="804">
<rule name="命令" toplevel="active">
<l>
<p>上网</p>
<p>计算器</p>
<p>记事本</p>
<p>空当接龙</p>
<p>命令结束</p>
</l>
</rule>
</grammar>

========在中文的机器上直接运行这个vbs,很有可能出现0x80045052错误,解决办法:
控制面板---语音-语音认别选项卡------------》改成microsoft simplified chinese

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网