(一)要使用系统的语音库,你需要先安装 Microsoft Speech SDK 5.1 及其语言包,下载地址:
Speech SDK 5.1: http://www.newhua.com/soft/38264.htm
Speech SDK 5.1 语言包(中英文): http://www.onlinedown.net/soft/38265.htm
(二)安装完成后,需要在D7里导入类库以便生成SAPI组件于IDE组件面板.
启动D7, Project -> Import Type Library->找到Microsoft Speech Object Library (Version 5.0)
点Install..
成功后会在D7的组件面板里出现 SAPI 5.1 控件页.
(三)OK,开始来个简单的工程.
启动D7新建个工程,往窗口里扔进一个TMemo,两个TButton,再扔进一个TSpVoice(SAPI 5.1控件页),写下下边的代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleServer, SpeechLib_TLB, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
SpVoice1: TSpVoice;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
procedure PlayVoxEx;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.PlayVoxEx;
var
i: Integer;
s,t: String;
begin
s:=Memo1.Text;
for i:=1 to Length(s) do
begin
if not (s[i] in [#10,#13]) then
begin
if ByteType(s, i)=mbSingleByte then
begin
SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(0); //设置报读的语种,0为英文,3为中文
t:=s[i];
end
else if ByteType(s, i)=mbLeadByte then
begin
SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(3);
t:=s[i];
t:=s[i]+s[i+1];
end
else
continue;
SpVoice1.Speak(t, SVSFlagsAsync); //读出Text
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(3);
SpVoice1.Speak(Memo1.Text, SVSFlagsAsync);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(0);
SpVoice1.Speak(Memo1.Text, SVSFlagsAsync);
end;
end.
Button1是直接读中文语音的,Button2可以读中英文混合(效果相当差),Button3直接读英文用的
我知道的就这么多了, 希望对你有点用...
这边的代码直接复制编译会报错, 需要按 CTRL+H 打开替换框, 复制" "双引号内的字符替换成两个空格(全部替换), 再编译就行了.
procedure TForm1.Button3Click(Sender: TObject);
begin
SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(0);
SpVoice1.Speak(Memo1.Text, SVSFlagsAsync);
end;
改成:
procedure TForm1.Button3Click(Sender: TObject);
begin
PlayVoxEx;
end;
安装成功之后再那里能找到TSpVoice ,我安装了之后再Delphi7里面多了这个页面 也不知道安装的对不对
dclusr.dpk是缺省的用户安装控件包, 多出来的应该是你之前安装到dclusr.dpk包里的一些别的控件... 在导入ActiveX那个界面里点Install后,你可以选 Into new package , 然后浏览一个目录填个包名再返回安装就不会有这些东西了.
TTS语音程序以下是全部源码,基本功能以实现
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,VTxtAuto_TLB, StdCtrls, Buttons, OleServer, SpeechLib_TLB, ComCtrls;type TForm1 = class(TForm) SpVoice1: TSpVoice; BitBtn1: TBitBtn; Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; BitBtn2: TBitBtn; BitBtn3: TBitBtn; BitBtn4: TBitBtn; BitBtn5: TBitBtn; ComboEngine: TComboBox; Button4: TButton; StatusBar1: TStatusBar; BitBtn6: TBitBtn; procedure BitBtn1Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure BitBtn2Click(Sender: TObject); procedure BitBtn3Click(Sender: TObject); procedure BitBtn4Click(Sender: TObject); procedure BitBtn5Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; tts:IVTxtAuto; implementation{$R *.dfm}procedure TForm1.BitBtn1Click(Sender: TObject); begin SpVoice1.Speak(Memo1.Lines.Text,SPFM_CREATE_ALWAYS);end;procedure TForm1.Button1Click(Sender: TObject); begin SpVoice1.Pause; end;procedure TForm1.Button2Click(Sender: TObject); begin SpVoice1.Resume; end;procedure TForm1.Button3Click(Sender: TObject); begin if SpVoice1.Rate<10 then SpVoice1.Rate:=SpVoice1.Rate+1; end;procedure TForm1.BitBtn2Click(Sender: TObject); begin if SpVoice1.Rate>-10 then SpVoice1.Rate:=SpVoice1.Rate-1; end;procedure TForm1.BitBtn3Click(Sender: TObject); begin if SpVoice1.Volume<100 thenSpVoice1.Volume:=SpVoice1.Volume+1; end;procedure TForm1.BitBtn4Click(Sender: TObject); begin if SpVoice1.Volume>0 thenSpVoice1.Volume:=SpVoice1.Volume-1; end;procedure TForm1.BitBtn5Click(Sender: TObject); var Sots1:ISpeechObjectTokens; Sot1:ISpeechObjectToken; i:integer; begin SpVoice1.EventInterests := SVEAllEvents; Sots1:=SpVoice1.GetVoices('',''); ComboEngine.Clear; for i := 0 to Sots1.Count-1 do begin Sot1:=Sots1.Item(i); ComboEngine.Items.Add(Sot1.GetDescription(0)); end; begin if ComboEngine.Items.Count > 0 then begin if ComboEngine.Items.IndexOf('Microsoft Simplified Chinese')=-1 then ComboEngine.ItemIndex := 0 else ComboEngine.ItemIndex := ComboEngine.Items.IndexOf('Microsoft Simplified Chinese'); end; end; end; procedure TForm1.Button4Click(Sender: TObject); var //设置可用引擎 Sots1:ISpeechObjectTokens; Sot1:ISpeechObjectToken; begin if ComboEngine.Itemindex=-1 then Exit; Sots1:=SpVoice1.GetVoices('',''); Sot1:=Sots1.Item(ComboEngine.Itemindex); SpVoice1.Voice:=Sot1; StatusBar1.Panels[0].Text:='朗读引擎:'+ComboEngine.Items.Strings[ComboEngine.Itemindex]; end; end.中英文混读方法
正常情况下,当一篇中文文档中有英文,并选择中文语音朗读时,英文朗读效果可能不理想;或者,一篇英文文档中有中文,选择英文语音朗读时,中文将无法朗读出来。解决的办法就是在文档中添加中英文语音标记(标记为:<lang langid="XXX"></lang>,在实际朗读中不会读出来),例如: <lang langid="804">中文句子</lang> 以上"中文句子"会强制用中文语音朗读 <lang langid="409">english sentenses</lang> 以上"english sentenses"会强制用英文语音朗读 标记中804代表中文,409代表英文。