delphi speak voice spvoice

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics,   Vcl.StdCtrls, Vcl.ExtCtrls,
  Vcl.OleServer, SpeechLib_TLB;

type
  TForm2 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    SpVoice1: TSpVoice;
    btn3: TButton;
    SpResourceManager1: TSpResourceManager;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}
uses ActiveX,Comobj;


procedure TForm2.btn1Click(Sender: TObject);
var
  s:string;
  voice: OLEVariant;
begin

  // CoInitialize(nil); //必须要有,不然不出声音
   try
     s:= '中国人民万岁';

    if s<>'' then
    begin
      voice := CreateOLEObject('sapi.spvoice');  //{96749373-3391-11D2-9EE3-00C04F797396}
      //voice := CreateOLEObject('96749373-3391-11D2-9EE3-00C04F797396');  //{96749373-3391-11D2-9EE3-00C04F797396}
      voice.Speak(s,0);
    end;
   finally
    // CoUninitialize; //无须注释掉,注释不注释,都有声音。
   end;
end;
procedure TForm2.btn2Click(Sender: TObject);
var
  s:string;
begin
   //CoInitialize(nil);
   try
     s:= '开始上学了';
    if s<>'' then
    begin
      SpVoice1.Voice:=SpVoice1.GetVoices('','').Item(0);
      SpVoice1.Speak(s, SVSFlagsAsync);
    end;
   finally
     //CoUninitialize; //必须注释掉,不然不出声音,可能是被释放掉了。
   end;
end;
 //SAPI.SpVoice
procedure TForm2.btn3Click(Sender: TObject);
var
  s:string;
  voice: OLEVariant;
begin

   CoInitialize(nil); //必须要有,不然不出声音
   try
     s:= '中国人民万岁';

    if s<>'' then
    begin
      voice := CreateOLEObject('SAPI.SpVoice');  //{96749373-3391-11D2-9EE3-00C04F797396}
      //voice := CreateOLEObject('96749373-3391-11D2-9EE3-00C04F797396');  //{96749373-3391-11D2-9EE3-00C04F797396}
      voice.Speak(s);
    end;
   finally
     CoUninitialize; //无须注释掉,注释不注释,都有声音。
   end;
end;


end.

猜你喜欢

转载自www.cnblogs.com/tobetterlife/p/12173081.html
今日推荐