当前位置: 源码素材网 » Delphi教程 » 详情页

Delphi实现电话录音模块的类代码

  •   时间:2019-07-10
  • 概述:录音 模块

Delphi实现录音功能的一个模块类代码,可实现电话录音,代码中的注释相当丰富,需要此功能的用户可不要错过,代码如下:

unit Pub_Unit;
interface
uses tc08a32, sysstr, Windows, Forms, Messages, SysUtils, Classes, Dialogs, DB,
ADODB,
  ExtCtrls, Graphics, DBGrids, StrUtils, Grids, StdCtrls, ComCtrls;
const
  Dir = '.\voc\';
  RecordFile = '.\Temp\';
  CONExit = -1; //退职出并释放通道
  NoFind = -2; //当审核不正确时播报提示信息
  CommingTele = 0; //获取来电显
  Welcome = 1; //播报欢迎词
  PersonCheck = 2; //审核是否为检察员
  ChannelCheck = 3; //审核通道是否可录音
  StartRecord = 4; //开始录音
  Recording = 5; //正在录音
  EndRecord = 6; //结束录音
  RecordMode = 7; //录音开启方式
  RecordKey = 8; //控制录音的按键
  ChannelFree = 9; //通道空闲
  ChannelInit = 10; //通道初始状态
type
  TChannelType = (TYPE_USER, TYPE_TRUNK, TYPE_EMPTY, TYPE_RECORD);
  TStartMode = (Ya_Kong, Sheng_Kong, Jian_Kong);
  ChannelPara = record //记录了通道的信息
    IsUse: Boolean; //是否占用
    InLineNo: Integer; //连通的内线通道号
    DtmfID: string; //该通道的缓冲区键值
    Step: Integer; //下次运行时的位置
    CallID: string; //来电号码
    CurrDtmfID: Integer; //按键缓冲区
    OutCallID: string; //回播电话号码
    RecordFile: string; //当前通道录音文件
    NewRecordFile: string; //当前通道录音文的存放目录
    StartDate: string; //录音开始时间
    EndDate: string; //录音结束时间
    CHType: TChannelType; //通道类型
    StartMode: TStartMode; //录音启动方式
  end;
var
  channelcount: Integer;
  channel: array[0..100] of channelpara;
  MaxRecord, MinRecord: Integer;
  RecordType: TStartMode;
  StartMode: TStartMode;
  i: Integer;
function Initdrv: Boolean; //设备初始化
procedure InitCH; //初始化通道
function Getchstate(chno: Integer): string; //获取通道类型
function GetCallID(chno: Integer): Boolean; //获取电话号
procedure AddPCM(chno: Integer; FileName: string; IsInit: Boolean);
  //添加语音文件
procedure PlayTelID(chno: Integer; TelID: string; IsInit: Boolean);
  //添加播放号码(0..9)
procedure TimerRun; //在Timer中运行
procedure TelFist(chno: integer); //来电话并未接电话时
procedure TelTurn(chno: integer); //电话在通话中
procedure paymsg(Chno: Integer); //进入语音卡功能模块
procedure PayExit(chno: Integer); //释放通道
function PersonChecks(var CallID: string): Boolean; //审核检察员
function ChannelChecks(Chno: Integer): Boolean; //审核通道是否可录音
procedure RecordTime; //录音时长
procedure FileCopy; //文件复制
procedure CreateADOQ(var ADOQ: TADOQuery; SQL: string; isOpen: Boolean = True);
  //创建数据集
function GetSec(Time: TDateTime): Integer; //获取秒数
function GetPath: string; //获取录音存放路径
function GerDirve(Dirve: string): Integer; //获取驱动器大小
function GetFile(ChanPara: channelpara; Path: string; Channel: Integer): string;
  //获取临时文件全路径
function GetNewFile(Path: string): string; //获取录音文件的存放路径
procedure SaveRecord(ChanPara: channelpara; chno: Integer);
  //将录音信息保存到数据库
function isSmallRecordLen(ChanPara: ChannelPara): Boolean;
  //当前录音长度是否小于指定录音长度
function GetRecordMode(StartMode: TStartMode): Boolean; //获取录音的开启方式
function GetStartCode: Integer; //获取开启录音的键值
function GetEndCode: Integer; //获取结束录音的键值
implementation
uses DMmains, Main;
{-------------------------------------------------------------------------------
  过程名:    Initdrv
  作者:      LiangShui
  日期:      2006.11.08
  参数:      无
  返回值:    Boolean
  功能:      初始化语音卡
-------------------------------------------------------------------------------}
function Initdrv: Boolean;
var
  IsLoad:Boolean;
begin
  IsLoad := False;
  if LoadDRV() = 0 then
    IsLoad := True;
  channelcount := CheckValidCh();
  EnableCard(channelcount, 1024 * 50);
  Sig_Init(0);
  Result := IsLoad;
  i:=0;
end;
{-------------------------------------------------------------------------------
  过程名:    InitCH
  作者:      LiangShui
  日期:      2006.11.21
  参数:      无
  返回值:    无
  功能:      初始化通道
-------------------------------------------------------------------------------}
procedure InitCH;
var
  i: Integer;
  chtype:Word;
begin
  for i := 0 to channelcount-1 do
  begin
    StartSigCheck(i);
    InitDtmfBuf(i);
    channel[i].IsUse := False;
    channel[i].InLineNo := -1;
    channel[i].DtmfID := '';
    channel[i].Step := ChannelInit;
    channel[i].CallID := '';
    channel[i].RecordFile := '';
    channel[i].NewRecordFile := '';
    channel[i].StartDate := '';
    channel[i].EndDate := '';
    chtype:=CheckChTypeNew(i);
    case  chtype of
      CHTYPE_USER:
        begin
          channel[i].CHType := TYPE_USER;
        end;
      CHTYPE_TRUNK:
        begin
          channel[i].CHType := TYPE_TRUNK;
        end;
      CHTYPE_EMPTY:
        begin
          channel[i].CHType := TYPE_EMPTY;
        end;
      CHTYPE_RECORD:
        begin
          channel[i].CHType := TYPE_RECORD;
        end;
    end;
    channel[i].StartMode := Jian_Kong;
//    if channel[i].CHType = TYPE_RECORD then
//    begin
//      if GetRecordMode(Ya_Kong) then
//        channel[i].StartMode := Ya_Kong
//      else if GetRecordMode(Sheng_Kong) then
//        channel[i].StartMode := Sheng_Kong
//      else if GetRecordMode(Jian_Kong) then
//        channel[i].StartMode := Jian_Kong
//      else
//        channel[i].StartMode := Ya_Kong;
//    end
//    else
//    begin
//      if GetRecordMode(Jian_Kong) then
//        channel[i].StartMode := Jian_Kong
//      else if GetRecordMode(Sheng_Kong) then
//        channel[i].StartMode := Sheng_Kong
//      else if GetRecordMode(Ya_Kong) then
//        channel[i].StartMode := Ya_Kong
//      else
//        channel[i].StartMode := Jian_Kong;
//    end;
  end;
end;
{-------------------------------------------------------------------------------
  过程名:    Getchstate
  作者:      LiangShui
  日期:      2006.11.08
  参数:      chno:Integer--通道号
  返回值:    string
  功能:      获取通道类型
-------------------------------------------------------------------------------}
function Getchstate(chno: Integer): string;
begin
  startsigcheck(chno);
  initdtmfbuf(chno);
  case CheckChTypeNew(chno) of
    CHTYPE_USER: Result := '内线';
    CHTYPE_TRUNK: Result := '外线';
    CHTYPE_EMPTY: Result := '悬空';
    CHTYPE_RECORD: Result := '录音模块';
  end;
end;
{-------------------------------------------------------------------------------
  过程名:    GetCallID
  作者:      LiangShui
  日期:      2006.11.08
  参数:      chno:Integer--通道号
  返回值:    Boolean
  功能:      获取电话号
-------------------------------------------------------------------------------}
function GetCallID(chno: Integer): Boolean;
var
  callID: PChar;
begin
  Result := False;
  GetMem(callID, 100);
  if (GetCallerIDStr(chno, callID) = 3) or
    (GetCallerIDStr(chno, callID) = 4) then
  begin
    channel[chno].CallID := RightStr(callID, Length(callID) - 8);
    offhook(chno);
    Result := True;
  end;
  FreeMem(callID);
end;
{-------------------------------------------------------------------------------
  过程名:    AddPCM
  作者:      LiangShui
  日期:      2006.11.08
  参数:      chno:Integer;FileName:string;IsInit:Boolean
                    通道号;语音文件名;是否初始化文件缓冲区
  返回值:    无
  功能:      添加语音文件
-------------------------------------------------------------------------------}
procedure AddPCM(chno: Integer; FileName: string; IsInit: Boolean);
begin
  if IsInit then
    rsetindexplayfile(chno);
  if FileExists(Dir + FileName) then
    addindexplayfile(chno, PChar(Dir + FileName {+'.pcm'}));
end;
{-------------------------------------------------------------------------------
  过程名:    PlayTelID
  作者:      LiangShui
  日期:      2006.11.08
  参数:      chno:Integer;TelID:string;IsInit:Boolean
                                    通道号;电话号码;是否初始化文件缓冲区
  返回值:    无
  功能:      添加播放号码(0..9)
-------------------------------------------------------------------------------}
procedure PlayTelID(chno: Integer; TelID: string; IsInit: Boolean);
var
  i: Integer;
begin
  if IsInit then
    rsetindexplayfile(chno);
  for i := 1 to Length(TelID) do
  begin
    case StrToInt(TelID[i]) of
      0: addindexplayfile(chno, Dir + 'D0');
      1: addindexplayfile(chno, Dir + 'D1');
      2: addindexplayfile(chno, Dir + 'D2');
      3: addindexplayfile(chno, Dir + 'D3');
      4: addindexplayfile(chno, Dir + 'D4');
      5: addindexplayfile(chno, Dir + 'D5');
      6: addindexplayfile(chno, Dir + 'D6');
      7: addindexplayfile(chno, Dir + 'D7');
      8: addindexplayfile(chno, Dir + 'D8');
      9: addindexplayfile(chno, Dir + 'D9');
    end;
  end;
end;
{-------------------------------------------------------------------------------
  过程名:    TimerRun
  作者:      LiangShui
  日期:      2006.11.08
  参数:      无
  返回值:    无
  功能:      在Timer中运行
-------------------------------------------------------------------------------}
procedure TimerRun;
var
  j:Integer;
begin
  push_play;
  feedsigfunc;
//  if i= channelcount then
//  i:=0;
  //i := 2;
 // while (i < channelcount) do
 // begin
    //      if channel[i].IsUse then
    //        begin
    //          TelTurn(i);
    //        end
    //      else if not channel[i].IsUse then
    //        begin
    //          if checkchtypenew(i) = CHTYPE_TRUNK then
    //            TelFist(i);
    //        end;
//    if i<channelcount then
//    begin
//    paymsg(i);
//    Inc(i);
//    end;
  //end;
  for j:=0 to channelcount-1 do
  begin
    paymsg(j);
  end;
end;
{-------------------------------------------------------------------------------
  过程名:    TelFist
  作者:      LiangShui
  日期:      2006.11.08
  参数:      chno:integer
  返回值:    无
  功能:      来电话并未接电话时
-------------------------------------------------------------------------------}
procedure TelFist(chno: integer);
begin
  if RingDetect(chno) then
  begin
    startsigcheck(chno);
    resetcalleridbuffer(chno);
    channel[chno].IsUse := True;
    channel[chno].Step := CommingTele;
    channel[chno].EndDate := '';
    starttimer(chno, 4);
    paymsg(chno);
  end
end;
{-------------------------------------------------------------------------------
  过程名:    TelTurn
  作者:      LiangShui
  日期:      2006.11.08
  参数:      chno:integer
  返回值:    无
  功能:      电话在通话中
-------------------------------------------------------------------------------}
procedure TelTurn(chno: integer);
begin
  //如果为外线并有忙音,以挂机
  StartSigCheck(chno);
  if (checkchtypenew(chno) = CHTYPE_TRUNK) and (sig_checkbusy(chno) = 1) then
  begin
    ClearLink(chno, channel[Chno].InLineNo);
    StopIndexPlayFile(channel[Chno].InLineNo);
    //FeedPower(channel[Chno].InLineNo);
    hangup(chno);
    //当录音记录存入数据库
    if channel[chno].EndDate = '' then
    begin
      StopRecordFile(Chno);
      channel[Chno].EndDate := QuotedStr(DateTimeToStr(Now));
      channel[chno].Step := CommingTele;
      SaveRecord(channel[chno], Chno);
    end;
    startsigcheck(chno); //开始新的信号检测
    Sig_ResetCheck(chno); //在挂机后执行,清空忙音缓冲区
    initdtmfbuf(chno);
    channel[chno].IsUse := false;
    exit;
  end;
  //内线,没有响铃
  if (ringdetect(chno) = false) and (checkchtypenew(chno) = CHTYPE_USER) then
  begin
    channel[chno].Step := CommingTele;
    startsigcheck(chno);
    StartPlaySignal(chno, SIG_STOP); //停止播放信号音
    initdtmfbuf(chno);
    channel[chno].IsUse := false;
    exit;
  end;
  paymsg(chno);
end;
{-------------------------------------------------------------------------------
  过程名:    paymsg
  作者:      LiangShui
  日期:      2006.11.08
  参数:      Chno:Integer
  返回值:    无
  功能:      进入语音卡功能模块
-------------------------------------------------------------------------------}
procedure paymsg(Chno: Integer);
var
  SLen: Integer;
begin
  push_play;
  feedsigfunc;
  case channel[Chno].Step of
    //初始状态
    ChannelInit:
      begin
        MainForm.cxListView1.Items[chno].Caption:='空闲状态';
        MainForm.cxListView1.Items[chno].ImageIndex := 0;
        startsigcheck(chno);
        //如果有电话
        if (checkchtypenew(chno) = CHTYPE_USER) and (ringdetect(Chno)=True)
          then
        begin
          channel[chno].Step := RecordMode;
          startsigcheck(chno);
          StartHangUpDetect(Chno);
          initdtmfbuf(chno);
        end
        else if RingDetect(chno) then
        begin
          MainForm.cxListView1.Items[chno].Caption:='有振铃';
          MainForm.cxListView1.Items[chno].ImageIndex := 1;
          resetcalleridbuffer(chno);
          channel[Chno].StartDate := '';
          channel[chno].EndDate := '';
          channel[chno].RecordFile := '';
          channel[chno].NewRecordFile := '';
          channel[chno].CallID := '';
          channel[chno].Step := CommingTele;
          initdtmfbuf(chno);
          //starttimer(chno, 4);
        end;
        //如果为外线并有忙音,以挂机
//          if (sig_checkbusy(chno) = 1) then
//            begin
//              ClearLink(chno, channel[Chno].InLineNo);
//              StopIndexPlayFile(channel[Chno].InLineNo);
//              FeedPower(channel[Chno].InLineNo);
//              hangup(chno);
//              startsigcheck(chno); //开始新的信号检测
//              Sig_ResetCheck(chno); //在挂机后执行,清空忙音缓冲区
//              initdtmfbuf(chno);
//            end
//          else
        //内线,没有响铃
      end;
    //获取来电
    CommingTele:
      begin
        if GetCallID(Chno) then
          channel[Chno].Step := Welcome;
      end;
    //播报欢迎词
    Welcome:
      begin
        MainForm.cxListView1.Items[chno].Caption:='电话接起';
        MainForm.cxListView1.Items[chno].ImageIndex := 2;
        AddPCM(Chno, 'Welcome', True);
        channel[chno].DtmfID := '';
        channel[chno].Step := PersonCheck;
        startindexplayfile(chno);
      end;
    //审核来电者是否为检察员
    PersonCheck:
      begin
        if CheckIndexPlayFile(chno) =0 then
          Exit;
        //审核检察员
        if not PersonChecks(channel[Chno].CallID) then
        begin
          AddPCM(Chno, 'NotRecordPopedom', True);
          channel[chno].Step := NoFind;
          startindexplayfile(chno);
        end
        else
        begin
          channel[chno].Step := ChannelCheck;
        end;
      end;
    //审核通道是否可录音
    ChannelCheck:
      begin
        CheckIndexPlayFile(chno);
        if not ChannelChecks(Chno) then
        begin
          AddPCM(Chno, 'NotRecord', True);
          channel[chno].Step := NoFind;
          startindexplayfile(chno);
        end
        else
        begin
          AddPCM(Chno, 'RecordHint', True);
          channel[chno].Step := RecordMode;
          startindexplayfile(chno);
        end;
      end;
    //录音开启方式
    RecordMode:
      begin
        MainForm.cxListView1.Items[chno].Caption:='电话接起';
        MainForm.cxListView1.Items[chno].ImageIndex := 2;
        if CheckIndexPlayFile(chno) = 0 then
          Exit;
        if channel[chno].StartMode = Ya_Kong then
        begin
          if DRec_OffHookDetect(Chno) then
            channel[chno].Step := StartRecord;
        end
        else if channel[chno].StartMode = Jian_Kong then
        begin
          if (DtmfHit(Chno)) then
          begin
            channel[Chno].DtmfID := IntToStr(GetDtmfCode(Chno));
            //ShowMessage(channel[chno].DtmfID);
            if channel[Chno].DtmfID = IntToStr(GetStartCode) then
            begin
              channel[chno].Step := StartRecord;
              InitDtmfBuf(Chno);
            end;
          end;
        end
        else if channel[chno].StartMode = Sheng_Kong then
        begin
          SLen := CheckSilence(Chno, 10);
          if SLen > 2 then
            channel[chno].Step := StartRecord;
        end;
        //以挂机
        if (sig_checkbusy(chno) = 1) then
        begin
          channel[Chno].Step := NoFind;
        end;
        //内线挂机
        if (channel[Chno].CHType = Type_User) and
            (HangUpDetect(Chno)=HANG_UP_FLAG_TRUE) then
        begin
          StartHangUpDetect(Chno);
          channel[Chno].Step := NoFind;
        end;
      end;
    //开始录音
    StartRecord:
      begin
        MainForm.cxListView1.Items[chno].ImageIndex := 3;
        MainForm.cxListView1.Items[chno].Caption:='正在录音';
        MainForm.cxListView1.Refresh;
        if CheckIndexPlayFile(chno) = 0 then
          Exit;
        //判断录音开启的方式
        //开始录音
        //InitDtmfBuf(Chno);
        RecordTime; //获取录音的最大、最小时长
        channel[Chno].StartDate := DateTimeToStr(Now); //录音开始时间
        channel[Chno].RecordFile := GetFile(channel[chno], RecordFile, Chno);
          //临时文件目录
        channel[Chno].NewRecordFile := GetNewFile(channel[Chno].RecordFile);
          //存放文件目录
        if StartRecordFile(Chno, PAnsiChar(channel[Chno].RecordFile), MaxRecord
          * 8000 + 58) then
        begin
          startsigcheck(chno);
          channel[chno].Step := Recording;
        end
        else
        begin
          AddPCM(Chno, 'LostRecord', True);
          channel[chno].Step := NoFind;
          startindexplayfile(chno);
        end;
      end;
    //正在录音
    Recording:
      begin
        if channel[Chno].StartMode = Ya_Kong then
        begin
          if not DRec_OffHookDetect(Chno) then
          begin
            channel[chno].Step := EndRecord;
          end;
        end
        else if channel[Chno].StartMode = Jian_Kong then
        begin
          if (DtmfHit(Chno)) and (GetDtmfCode(Chno) = GetEndCode) then
          begin
            channel[chno].Step := EndRecord;
            InitDtmfBuf(Chno);
          end;
        end
        else if channel[Chno].StartMode = Sheng_Kong then
        begin
          SLen := CheckSilence(Chno, 312);
          if (SLen >= 0) and (SLen < 5) then
            channel[Chno].Step := EndRecord;
        end;
        //录音文件到指定长度
        if CheckRecordEnd(Chno) then
        begin
          channel[chno].Step := EndRecord;
        end;
        //外线以挂机
        if (sig_checkbusy(chno) = 1) then
        begin
          channel[Chno].Step := EndRecord;
        end;
        //InitDtmfBuf(Chno);
        //内线挂机
        if (channel[Chno].CHType = Type_User) and
            (HangUpDetect(Chno)=HANG_UP_FLAG_TRUE) then
        begin
            StartHangUpDetect(Chno);
            channel[chno].Step := EndRecord;
        end;
      end;
    //结束录音
    EndRecord:
      begin
        MainForm.cxListView1.Items[chno].Caption:='录音结束';
        MainForm.cxListView1.Items[chno].ImageIndex := 4;
        MainForm.cxListView1.Refresh;
        StopRecordFile(Chno);
        AddPCM(Chno, 'OverRecord', True);
        channel[Chno].EndDate := DateTimeToStr(Now);
        channel[chno].Step := NoFind;
        startindexplayfile(chno);
        if( not isSmallRecordLen(channel[chno])) and
          (channel[Chno].CHType <> Type_User) then
          SaveRecord(channel[chno], Chno);
        //FileCopy(channel[chno].RecordFile);
      end;
    //审核不正确播报提示音
    NoFind:
      begin
        if CheckIndexPlayFile(chno) = 0 then
          Exit;
        if (channel[Chno].CHType = Type_User) and
            (HangUpDetect(Chno)=HANG_UP_FLAG_TRUE) then
            channel[chno].Step := CONExit
        else if (channel[Chno].CHType <> Type_User) then
            begin
              channel[chno].Step := CONExit;
              hangup(chno);
            end
        else if (channel[Chno].CHType <> Type_User) and
          (sig_checkbusy(chno) = 1) then
        begin
            ClearLink(chno, channel[Chno].InLineNo);
            StopIndexPlayFile(channel[Chno].InLineNo);
            FeedPower(channel[Chno].InLineNo);
            hangup(chno);
            startsigcheck(chno); //开始新的信号检测
            Sig_ResetCheck(chno); //在挂机后执行,清空忙音缓冲区
            initdtmfbuf(chno);
            channel[chno].Step := CONExit;
        end;
      end;
    ChannelFree:
      begin
      end;
    //退出并释放通道
    CONExit:
      begin
        PayExit(Chno);
      end;
  end;
end;
{-------------------------------------------------------------------------------
  过程名:    PayExit
  作者:      LiangShui
  日期:      2006.11.08
  参数:      chno:Integer
  返回值:    无
  功能:      释放通道
-------------------------------------------------------------------------------}
procedure PayExit(chno: Integer);
begin
  if CheckIndexPlayFile(chno) <> 0 then
  begin
    startsigcheck(chno);
    //hangup(chno);
    initdtmfbuf(chno);
    Sig_ResetCheck(chno);
    channel[chno].Step := ChannelInit;
    channel[Chno].DtmfID := '';
    channel[Chno].CallID := '';
    channel[Chno].InLineNo := 0;
  end;
end;
{-------------------------------------------------------------------------------
  过程名:    PersonCheck
  作者:      LiangShui
  日期:      2006.11.16
  参数:      TelID:String
  返回值:    Boolean
  功能:      审核来电者是否为检察员
-------------------------------------------------------------------------------}
function PersonChecks(var CallID: string): Boolean;
var
  SQL: string;
  ADOQ: TADOQuery;
begin
  SQL := 'Select ID,Phone From MR_Number Where Phone =' + QuotedStr(CallID);
  CreateADOQ(ADOQ, SQL);
  if ADOQ.RecordCount > 0 then
  begin
    CallID := ADOQ.FieldByName('ID').AsString;
    Result := True;
  end
  else
    Result := False;
  ADOQ.Free;
end;
{-------------------------------------------------------------------------------
  过程名:    ChannelCheck
  作者:      LiangShui
  日期:      2006.11.16
  参数:      Chno:Integer
  返回值:    Boolean
  功能:      审核通道是否可录音
-------------------------------------------------------------------------------}
function ChannelChecks(Chno: Integer): Boolean;
var
  SQL: string;
  ADOQ: TADOQuery;
begin
  SQL := 'Select M.ID From MR_Line M,MR_Line_User D ' +
    ' Where M.Channel=' + IntToStr(Chno) + ' and M.Useflg=1 and ' +
    ' M.ID=D.LineID and D.UserID=' + DMmain.varPublic.EmplID + ' and ' +
    ' D.Useflg=1 and D.isRecord = 1';
  CreateADOQ(ADOQ, SQL);
  if ADOQ.RecordCount > 0 then
    Result := True
  else
    Result := False;
  ADOQ.Free;
end;
{-------------------------------------------------------------------------------
  过程名:    RecordMinTime
  作者:      LiangShui
  日期:      2006.11.17
  参数:      无
  返回值:    string
  功能:      录音最小时长
-------------------------------------------------------------------------------}
procedure RecordTime;
var
  Max, Min: TDateTime;
  ADOQ: TADOQuery;
  SQL: string;
begin
  SQL := 'Select * From MR_Time';
  CreateADOQ(ADOQ, SQL);
  if ADOQ.RecordCount > 0 then
  begin
    Max := ADOQ.FieldByName('TimeMax').AsDateTime;
    Min := ADOQ.FieldByName('TimeMin').AsDateTime;
    MaxRecord := GetSec(Max);
    MinRecord := GetSec(Min);
  end
  else
  begin
    MaxRecord := 60;
    MinRecord := 0;
  end;
end;
{-------------------------------------------------------------------------------
  过程名:    FileCopy
  作者:      LiangShui
  日期:      2006.11.17
  参数:      FileName:String
  返回值:    无
  功能:      将临时文件复制到备份目录
-------------------------------------------------------------------------------}
procedure FileCopy;
var
  OldFile, NewFile: string;
  ADOQ: TADOQuery;
  SQL: string;
  i:Integer;
begin
  SQL := 'Select * From MR_RecordTempFile Where Len(OldFile)>0 and Len(NewFile)>0 ';
  CreateADOQ(ADOQ, SQL);
  if ADOQ.IsEmpty then
    Exit;
  for i:=0 to ADOQ.RecordCount-1 do
  begin
    OldFile := ADOQ.FieldByName('OldFile').AsString;
    NewFile := ADOQ.FieldByName('NewFile').AsString;
    try
      CopyFile(PAnsiChar(OldFile), PAnsiChar(NewFile), False);
      if FileExists(NewFile) then
      begin
        ADOQ.Delete;
        DeleteFile(OldFile);
      end;
    except
    end;
    ADOQ.Next;
  end;
  ADOQ.Free;
end;
{-------------------------------------------------------------------------------
  过程名:    CreateADOQ
  作者:      LiangShui
  日期:      2006.11.17
  参数:      var ADOQ:TADOQuery;SQL:String;isOpen:Boolean
  返回值:    无
  功能:      创建数据集
-------------------------------------------------------------------------------}
procedure CreateADOQ(var ADOQ: TADOQuery; SQL: string; isOpen: Boolean = True);
begin
  ADOQ := TADOQuery.Create(nil);
  ADOQ.Connection := DMmain.ADOConn1;
  ADOQ.SQL.Text := SQL;
  if isOpen then
    ADOQ.Open
  else
    ADOQ.ExecSQL;
end;
{-------------------------------------------------------------------------------
  过程名:    GetSec
  作者:      LiangShui
  日期:      2006.11.17
  参数:      Time:TDateTime
  返回值:    Integer
  功能:      返回秒数
-------------------------------------------------------------------------------}
function GetSec(Time: TDateTime): Integer;
var
  H, M, S, MS: Word;
begin
  DecodeTime(Time, H, M, S, MS);
  Result := H * 60 * 60 + M * 60 + S;
end;
{-------------------------------------------------------------------------------
  过程名:    GetPath
  作者:      LiangShui
  日期:      2006.11.17
  参数:      无
  返回值:    string
  功能:      获取录音存放路径
-------------------------------------------------------------------------------}
function GetPath: string;
var
  Path, PathTemp: string;
  Max, Temp: Integer;
  ADOQ: TADOQuery;
  SQL: string;
  i: Integer;
  Dirve: string;
begin
  Path := '';
  Max := 0;
  SQL := 'Select * From MR_Dir Where Useflg = 1';
  CreateADOQ(ADOQ, SQL);
  for i := 0 to ADOQ.RecordCount - 1 do
  begin
    PathTemp := ADOQ.FieldByName('Path').AsString;
    Dirve := ExtractFileDrive(PathTemp);
    Temp := GerDirve(Dirve);
    if Temp > Max then
    begin
      Max := Temp;
      Path := PathTemp;
    end;
  end;
  Result := Path;
end;
{-------------------------------------------------------------------------------
  过程名:    GerDirve
  作者:      LiangShui
  日期:      2006.11.18
  参数:      Dirve:string
  返回值:    Integer
  功能:      获取驱动器大小
-------------------------------------------------------------------------------}
function GerDirve(Dirve: string): Integer;
var
  S1, S2, S3, S4: Cardinal;
begin
  try
    GetDiskFreeSpace(PAnsiChar(Dirve), S1, S2, S3, S4);
    Result := S1 * S2 * s3;
  except
    Result := 0;
  end
end;
{-------------------------------------------------------------------------------
  过程名:    GetFile
  作者:      LiangShui
  日期:      2006.11.18
  参数:      Path:string;CallID:string;Channel:Integer
  返回值:    string
  功能:      生成录音文件名
-------------------------------------------------------------------------------}
function GetFile(ChanPara: channelpara; Path: string; Channel: Integer): string;
var
  NewFileName: string;
  Temp: string;
begin
  Temp := ChanPara.StartDate;
  Temp := StringReplace(Temp, ':', '', [rfReplaceAll]);
  Temp := StringReplace(Temp, '-', '', [rfReplaceAll]);
  Temp := StringReplace(Temp, ' ', '', [rfReplaceAll]);
  if RightStr(Path,1)='\' then
    NewFileName := Path + Temp{+ChanPara.CallID }+ IntToStr(Channel)
  else
    NewFileName := Path + '\'+Temp+ChanPara.CallID + IntToStr(Channel);
  Randomize;
  NewFileName := NewFileName + IntToStr(Random(9));
  Randomize;
  NewFileName := NewFileName + IntToStr(Random(9));
  NewFileName := NewFileName + '.Wav';
  Result := NewFileName;
end;
{-------------------------------------------------------------------------------
  过程名:    GetNewFile
  作者:      LiangShui
  日期:      2006.11.18
  参数:      Path:string
  返回值:    string
  功能:      获取录音文件的存放路径
-------------------------------------------------------------------------------}
function GetNewFile(Path: string): string;
var
  Paths:string;
begin
  Paths := GetPath;
  if RightStr(Paths,1)='\' then
    Result := Paths+ExtractFileName(Path)
  else
    Result := Paths+'\'+ ExtractFileName(Path);
end;
{-------------------------------------------------------------------------------
  过程名:    SaveRecord
  作者:      LiangShui
  日期:      2006.11.18
  参数:      ChanPara:channelpara
  返回值:    无
  功能:      将录音文件保存到数据库和声音文件临时表
-------------------------------------------------------------------------------}
procedure SaveRecord(ChanPara: channelpara; chno: Integer);
var
  ADOQ: TADOQuery;
  SQL: string;
  NewFile: string;
begin
  //@Channel,@StartDate,@EndDate,@InOut,@LocalPath,
  //@Path,@Operator,@Phone
  NewFile := ExtractFilePath(ChanPara.NewRecordFile);
  NewFile := LeftStr(NewFile, Length(NewFile) - 1);
  SQL := ' Exec P_SaveRecord ' +
    IntToStr(chno) + ',' +
    QuotedStr(ChanPara.StartDate) + ',' +
    QuotedStr(ChanPara.EndDate) + ',0,' +
    QuotedStr(NewFile) + ',' +
    DMmain.varPublic.EmplID + ',' +
    QuotedStr(ChanPara.CallID) + ',' +
    QuotedStr(ChanPara.RecordFile) + ',' +
    QuotedStr(ChanPara.NewRecordFile)+','+
    QuotedStr(ExtractFileName(ChanPara.NewRecordFile));
  CreateADOQ(ADOQ, SQL, False);
  ADOQ.Free;
end;
{-------------------------------------------------------------------------------
  过程名:    isSmallRecordLen
  作者:      LiangShui
  日期:      2006.11.18
  参数:      ChanPara:ChannelPara
  返回值:    Boolean
  功能:      当前录音长度是否小于指定最小录音长度
-------------------------------------------------------------------------------}
function isSmallRecordLen(ChanPara: ChannelPara): Boolean;
var
  StartDate: TDateTime;
  EndDate: TDateTime;
begin
  StartDate := StrToDateTime(ChanPara.StartDate);
  EndDate := StrToDateTime(ChanPara.EndDate);
  if GetSec(EndDate) - GetSec(StartDate) > MinRecord then
    Result := False
  else
    Result := True;
end;
{-------------------------------------------------------------------------------
  过程名:    GetRecordMode
  作者:      LiangShui
  日期:      2006.11.20
  参数:      无
  返回值:    TRecordType  rtKey,rtSount,rtPress  键控,声控,压控
  功能:      获取录音的开启方式
-------------------------------------------------------------------------------}
function GetRecordMode(StartMode: TStartMode): Boolean;
var
  ADOQ: TADOQuery;
  SQL: string;
begin
  Result := False;
  SQL := 'Select * From MR_Start';
  CreateADOQ(ADOQ, SQL);
  if ADOQ.RecordCount > 0 then
  begin
    if StartMode = Ya_Kong then
      Result := ADOQ.FieldByName('Useflg1').AsBoolean
    else if StartMode = Jian_Kong then
      Result := ADOQ.FieldByName('Useflg2').AsBoolean
    else if StartMode = Sheng_Kong then
      Result := ADOQ.FieldByName('Useflg3').AsBoolean;
  end;
end;
{-------------------------------------------------------------------------------
  过程名:    GetStartCode
  作者:      LiangShui
  日期:      2006.11.20
  参数:      无
  返回值:    Integer
  功能:      //获取录音启动键
-------------------------------------------------------------------------------}
function GetStartCode: Integer;
var
  ADOQ: TADOQuery;
  SQL: string;
  Code: string;
begin
  SQL := 'Select * From MR_Start';
  CreateADOQ(ADOQ, SQL);
  Code := Trim(ADOQ.FieldByName('Parame2_1').AsString);
  if Code = '*' then
  Result := 11
  else
  Result := -1;
end;
{-------------------------------------------------------------------------------
  过程名:    GetEndCode
  作者:      LiangShui
  日期:      2006.11.20
  参数:      无
  返回值:    Integer
  功能:      获取结束录音的键值
-------------------------------------------------------------------------------}
function GetEndCode: Integer;
var
  ADOQ: TADOQuery;
  SQL: string;
  Code: string;
begin
  SQL := 'Select * From MR_Start';
  CreateADOQ(ADOQ, SQL);
  Code := Trim(ADOQ.FieldByName('Parame2_2').AsString);
  if Code = '#' then
    Result := 12
  else
    Result := -1;
end;
end.

    相关声明:

      若“Delphi实现电话录音模块的类代码”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。