加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 使用Dephi的SAPI:异步语音不起作用

发布时间:2020-12-15 09:25:12 所属栏目:大数据 来源:网络整理
导读:以下工作完美(Delphi 7): procedure TMainForm.SayIt(s:string); // s is the string to be spokenvarvoice: OLEVariant;begin memo1.setfocus; voice := CreateOLEObject ('SAPI.SpVoice'); voice.Voice := voice.GetVoices.Item(combobox1.ItemIndex); //
以下工作完美(Delphi 7):

procedure TMainForm.SayIt(s:string); // s is the string to be spoken
var
voice: OLEVariant;
begin
  memo1.setfocus;
  voice := CreateOLEObject ('SAPI.SpVoice');
  voice.Voice := voice.GetVoices.Item(combobox1.ItemIndex); // current voice selected
  voice.volume := tbVolume.position;
  voice.rate := tbRate.position;
  voice.Speak (s,SVSFDefault);
end;

以上工作在“同步”模式(SVSFDefault标志),但如果我在尝试以异步模式播放声音时将标志更改为SVSFlagsAsync,则不会产生声音.没有给出错误消息,但扬声器上没有播放任何内容.

问题可能是什么?我在Delphi的Imports文件夹中有SpeechLib_TLB单元.

编辑:这是在Windows XP中

谢谢,
布鲁诺.

解决方法

当您使用 SVSFlagsAsync标志时,语音流在内部缓冲区中排队并等待语音服务执行,所以我认为您发出的与语音对象的生命周期有关,因为它是一个局部变量,实例在执行声音之前被销毁.

作为解决方法,您可以使用WaitUntilDone方法等待声音

voice.Speak (s,SVSFlagsAsync);
  repeat  Sleep(100); until  voice.WaitUntilDone(10);

或者在表单定义中声明语音变量.

TMainForm = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
   voice: OLEVariant;
   procedure SayIt(const s:string);
  end;

var
  MainForm: TMainForm;

implementation

uses
   ComObj;

{$R *.dfm}
procedure TMainForm.SayIt(const s:string); // s is the string to be spoken
const
  SVSFDefault = 0;
  SVSFlagsAsync = 1;
  SVSFPurgeBeforeSpeak= 2;
begin
  memo1.setfocus;
  voice.Voice := voice.GetVoices.Item(combobox1.ItemIndex); // current voice selected
  voice.volume := tbVolume.position;
  voice.rate := tbRate.position;
  voice.Speak (s,SVSFlagsAsync {or SVSFPurgeBeforeSpeak});
end;


procedure TMainForm.Button1Click(Sender: TObject);
begin
  SayIt('Hello');
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  voice := CreateOLEObject('SAPI.SpVoice');
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  voice  := Unassigned;
end;

end.

另外请注意,由于您使用的是后期绑定,因此不需要SpeechLib_TLB单元.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读