⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 speechdefineunit.pas

📁 采用多线程方式朗读中文文档
💻 PAS
字号:
unit SpeechdefineUnit;

interface
  Uses Classes,Windows,SysUtils,StrUtils,Forms,Dialogs,SpeechThreadUnit;
  type
  TSpeechChinese = Class(TObject)
  Private
    FbStopRead   : boolean;
    FCanRead     : boolean;
    //FIsReading   : boolean;
    mSpeechthd   : TSpeechThread;
  protected
    function GetCanRead():boolean;
    function GetIsReading():boolean;
  public
    procedure Terminate();
    procedure SpeechArticle(aList:TStrings); overload;
    procedure SpeechArticle(StrSpeech:String);overload;
    function ChnCurrency(nn: Double): String;
    procedure StopSpeech();
    constructor Create;
    destructor Destory;
  published
    property CanRead :boolean Read GetCanRead;
    property IsReading :boolean Read GetIsReading;
  end;
  
implementation
{  //使用方法
   第1步:创建对象
   mSpeech:=TSpeechChinese.Create;
   第2步:
   if mSpeech.CanRead then
   begin  //判断是否装在语音库成功
      if mSpeech.IsReading then
      begin  //判断是否正在读
         MessageDlg('正在读,请稍候',mtInformation, [mbOk], 0);
         Exit;
      end;
      //将字符串列表传入
      mSpeech.SpeechArticle(Memo1.Lines);
   end;
   停止正在读的语音
   if mSpeech.IsReading then
      mSpeech.StopSpeech();
   释放语音库
    mSpeech.Terminate ;  //必须先执行Terminate方法
    FreeandNil(mSpeech);
    问题:阿拉伯数字和字母符号必须转换成中文才能发音
}
////////////////////////////////////////////////////////////////////////////////
constructor TSpeechChinese.Create;
begin
  Inherited Create;

  mSpeechthd:=TSpeechThread.Create(false);
end;

destructor TSpeechChinese.Destory;
begin
  StopSpeech();
  if mSpeechthd<>nil then
  begin
    mSpeechthd.Terminate ;
    Sleep(100);
  
    mSpeechthd:=nil;
  end;
  inherited;
end;
//释放阅读线程
//在退出程序前必须先调用此函数
procedure TSpeechChinese.Terminate();
begin
  StopSpeech();
  if mSpeechthd<>nil then
  begin
    mSpeechthd.Terminate ;
    Sleep(100);

    mSpeechthd:=nil;
  end;
end;

//判断是否能够阅读
function TSpeechChinese.GetCanRead():boolean;
begin
  Result:=false;
  FCanRead:=false;
  if mSpeechthd=nil then exit;
  FCanRead:= mSpeechthd.bCanRead ;
  Result:=mSpeechthd.bCanRead ;
end;

//判断是否正在阅读
function TSpeechChinese.GetIsReading():boolean;
begin
  Result:=false;
  if mSpeechthd=nil then exit;
  Result:= mSpeechthd.IsbReading() ;
end;

//阅读队列中的文字
procedure TSpeechChinese.SpeechArticle(aList:TStrings);
begin
  if mSpeechthd=nil then exit;
  if mSpeechthd.bReading then
  begin
    MessageDlg('正在朗读,请稍候.',mtInformation, [mbOk], 0);
    Exit;
  end;
  mSpeechthd.SetSpeechList(aList);
 
end;

//阅读文本串中的文字
procedure TSpeechChinese.SpeechArticle(StrSpeech:String);
var
  i:Integer;
  sList:TStrings;
begin

  if mSpeechthd=nil then exit;
  if mSpeechthd.bReading then
  begin
    MessageDlg('正在朗读,请稍候.',mtInformation, [mbOk], 0);
    Exit;
  end;
  sList:=TStringlist.Create ;
  try
    sList.Clear ;
    sList.Add(StrSpeech); 
    mSpeechthd.SetSpeechList(sList);
  finally
    sList.Free ;
  end;
end;

//停止正在阅读的语音
procedure TSpeechChinese.StopSpeech();
begin
  if mSpeechthd=nil then exit;
  FbStopRead:= true;
  mSpeechthd.StopSpeech();
end;

//将阿拉伯数字转换为中文大写数字
function TSpeechChinese.ChnCurrency(nn: Double): String;
const   {将阿拉伯数字转换为中文大写数字}
  ChnDgt: array[48..57] of string=('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
  ChnPwr: array[1..12] of string=('仟','佰','拾','亿','仟','佰','拾','万','仟','佰','拾','');
//                                 0    1    2    3    4    5    6    7    8    9   10
var
  bSkip: Boolean;
  NumChrs: Pchar;
  rmb,Strdecimalfraction,StrInt: String;
  Str1,Str2,Str:String;
  i,nLen,iPos: Smallint;
begin
// 对0处理
  if (nn<=0.0000001)and(nn>=-0.0000001) then
  begin
    Result:='零';
    exit;
  end;
//对负数进行处理
  if nn<-0.0000001 then
  begin
    rmb:='负';
    nn:= -nn;
  end
  else rmb:='';

  bSkip:=False;
  Result:='';
// 对超大数进行处理,  最大数应小于1亿亿
  if nn>=10000000000.00 then
  begin
    Result:= FloatToStr(nn);
    exit;
  end;
  Str:=Format('%11.4f',[nn]);
  Str:=Trim(Str);
  nLen:=Length(Str);

  Strdecimalfraction:=RightStr(Str,4); //小数
  StrInt:=LeftStr(Str,nLen-5);             //整数
  if Strdecimalfraction[4]='0' then
  begin  //去掉小数后面的零
     Strdecimalfraction:=LeftStr(Strdecimalfraction,3);
     if Strdecimalfraction[3]='0' then
     begin
        Strdecimalfraction:=LeftStr(Strdecimalfraction,2);
        if Strdecimalfraction[2]='0' then
        begin
            Strdecimalfraction:=LeftStr(Strdecimalfraction,1);
            if Strdecimalfraction='0' then Strdecimalfraction:='';
        end;
     end;
  end;

  Str1:='';
  if Strdecimalfraction<>'' then
  begin
    For i:=1 to Length(Strdecimalfraction) do
    begin
      Str1:= Str1+ ChnDgt[Smallint(Strdecimalfraction[i])];
    end;
  end;
  //补齐长度
  for i:=1 to 12-nLen+5 do
      StrInt:=' '+StrInt;
  Str2:='';

  for i:=1 to 12 do
  begin
    if StrInt[i]=' ' then Continue;
    if StrInt[i]='0' then
    begin
      bSkip:=true;
      if i=4 then Str2:=Str2+ChnPwr[i];   {亿}
      if i=8 then Str2:=Str2+ChnPwr[i];   //万
      Str2:=AnsiReplaceText(Str2, '亿万', '亿');//去除“亿万”中的“万”字
    end
    else
    begin
      if bSkip then Str2:=Str2+'零';
      
      bSkip:=false;
      Str2:=Str2+ ChnDgt[Smallint(StrInt[i])];

      Str2:=Str2+ChnPwr[i];

    end;
     
  end;
  if Str1<>'' then
  begin
    Result:=rmb+Str2+'点'+Str1;
  end
  else
  begin
    Result:=rmb+Str2
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -