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

📄 unit1.~pas

📁 语音modem 自动应答的demo程序,可按键录音
💻 ~PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, AdTapi, OoMisc, AdPort, ExtCtrls;

type
  TForm1 = class(TForm)
    ApdComPort1: TApdComPort;
    ApdTapiDevice1: TApdTapiDevice;
    LBSysInfo: TListBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ApdTapiDevice1TapiConnect(Sender: TObject);
    procedure ApdTapiDevice1TapiDTMF(CP: TObject; Digit: Char;
      ErrorCode: Integer);
    procedure ApdTapiDevice1TapiWaveNotify(CP: TObject; Msg: TWaveMessage);
    procedure Timer1Timer(Sender: TObject);
    procedure clickk;

  private
    { Private declarations }

  public
    { Public declarations }

  end;

var
  Form1: TForm1;
  CurrentState:(csIdle,csWaiting,csConnected,csPlaying, csRecording,sDisconnected);

implementation

{$R *.dfm}
procedure TForm1.clickk;
begin
 ApdTapiDevice1.CancelCall;
LBSysInfo.Items.Add('挂断对方电话');
CurrentState := csIdle;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  try
ApdTapiDevice1.EnableVoice := True;
except
Application.MessageBox('当前设备不支持语音扩展', '错误', MB_OK);
end;

if ApdTapiDevice1.EnableVoice then
begin
ApdTapiDevice1.AutoAnswer;
LBSysInfo.Items.Add('设备就绪可接听对方电话');
CurrentState := csWaiting;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   ApdTapiDevice1.SelectDevice;
   ApdTapiDevice1.EnableVoice := True;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
// ApdTapiDevice1.PlayWaveFile('.\Ring.WAV');
ApdTapiDevice1.CancelCall;
LBSysInfo.Items.Add('挂断对方电话');
CurrentState := csIdle;
 end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  CurrentState := csIdle;
ApdTapiDevice1.TrimSeconds := 5; //录音时有5秒静音就挂断
end;

procedure TForm1.ApdTapiDevice1TapiConnect(Sender: TObject);
begin
 CurrentState := csConnected;
LBSysInfo.Items.Add(':连接成功');
ApdTapiDevice1.PlayWaveFile('Welcome.wav');//播放功能提示语音
LBSysInfo.Items.Add(':播放Ring.WAV');
end;

procedure TForm1.ApdTapiDevice1TapiDTMF(CP: TObject; Digit: Char;
  ErrorCode: Integer);
  var
strr:string;
begin
  if (Digit ='') or (Digit = '') then
Exit;
strr:=strr+Digit;
LBSysInfo.Items.Add('dtmf:按键='+ strr);

//CurrentCommand:=CurrentCommand Digit;
{简单状态机}
if strr<>'' then

begin
if strr = '*#' then
begin
//CurrentCommand := '';
ApdTapiDevice1.MaxMessageLength := 30; //最长记录时间30秒
ApdTapiDevice1.InterruptWave := False; //按键不能中断提示语音的播放
ApdTapiDevice1.PlayWaveFile('Welcome.wav');//播放录音提示语音
CurrentState := csRecording;
LBSysInfo.Items.Add('luyingzhubei');
Exit;
end;

if Digit='1' then
begin
ApdTapiDevice1.PlayWaveFile('play1.wav');
LBSysInfo.Items.Add(Format('%s %s 正在播放 %s',[ApdTapiDevice1.calleridname, apdtapidevice1.callerid,'play1.wav']));
end
else
begin
//播放错误提示语音,并要求用户重新输入命令
ApdTapiDevice1.PlayWaveFile('Greeting.wav');
LBSysInfo.Items.Add(Format('%s %s 输入了错误的号码',[ApdTapiDevice1.calleridname, apdtapidevice1.callerid]));
end;
//重置命令为空
//CurrentCommand := '';
end;
end;

procedure TForm1.ApdTapiDevice1TapiWaveNotify(CP: TObject;
  Msg: TWaveMessage);
var
//TimeStr: string;
FileName: string;
begin
//决不能在case外做耗时的操作
case Msg of
waPlayOpen: LBSysInfo.Items.Add('wavnotify:播放开始');
waPlayDone:
begin
LBSysInfo.Items.Add('wavnotify:播放结束');
if CurrentState = csRecording then
begin
try
while ApdTapiDevice1.WaveState <> wsIdle do
Application.ProcessMessages;
ApdTapiDevice1.InterruptWave := True;
ApdTapiDevice1.StartWaveRecord;
LBSysInfo.Items.Add('dtmf:录音成功');
except
LBSysInfo.Items.Add('dtmf:录音失败');
end;
end;
end;
waPlayClose: LBSysInfo.Items.Add('wavnotify:播放关闭');
waRecordOpen: LBSysInfo.Items.Add('wavnotify:录音开始');
waDataReady:
begin
LBSysInfo.Items.Add('wavnotify:数据准备');
TimeSeparator := '-';
FileName := DateTimeToStr(Now)+ '.wav';
try
ApdTapiDevice1.SaveWaveFile(ExtractFilePath(ParamStr(0)) +FileName, True);
LBSysInfo.Items.Add('wavNotify:保存声音文件 '+ FileName);
except
LBSysInfo.Items.Add('wavnotify:保存声音文件失败');
end;
end;
waRecordClose:
LBSysInfo.Items.Add('wavnotify:记录声音结束');
//CurrentState:= csWaiting;
//ActionCancelExecute(nil);
//timer1.
end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin

clickk;
//CurrentState := csWaiting;
//Timer1.Enabled := False;

end;

end.

⌨️ 快捷键说明

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