3、看主要部分的源码: // // 开始录音... procedure TMainForm.Button2Click(Sender: TObject); begin // // 先停止可能的发声... if Assigned(Speaker) then if Assigned(Speaker.Media) then Speaker.Stop; // // 开始录音... Microphone := TCaptureDeviceManager.Current.DefaultAudioCaptureDevice; if Assigned(Microphone) then begin Microphone.FileName := TPath.GetHomePath+PathDelim+ 'tmpsend.3GP'; try Microphone.StartCapture; Image2.Visible := True; except Image2.Visible := False; ShowMessage('错误:不支持录音!'); end; end else ShowMessage('错误:麦克风不存在!'); end;
// // 停止录音... procedure TMainForm.Button3Click(Sender: TObject); var MailId: string; Parcel: TMBParcel; Stream: TMemoryStream; begin if Assigned(Microphone) then try if Microphone.State = TCaptureDeviceState.Capturing then begin Microphone.StopCapture; Image2.Visible := False; // // 生成音频文件... Stream:=TMemoryStream.Create; Stream.LoadFromFile(TPath.GetHomePath+PathDelim+ 'tmpsend.3GP'); if stringgrid2.RowCount=0 then stringgrid2.RowCount:=1; stringgrid2.Cells[0,0]:='最后发送的语音 - '+inttostr(Stream.Size)+'字节'; // // 分配Id... if not MainForm.mba.GenerateId('MailBox','MailId','SUBSTRING(MAILID,1,6)='''+formatdatetime('yymmdd',now)+'''','YYMMDDXXXX','',MailId) then begin showmessage('无法取消息号,发送失败!'); exit; end; // // 保存消息到收件箱... Parcel:=TMBParcel.Create; Parcel.PutStringGoods('MailId',MailId); Parcel.PutStringGoods('MailType','9'); Parcel.PutStringGoods('UserId',''); Parcel.PutStringGoods('UserName',''); Parcel.PutStringGoods('FromUserId',MainForm.s_UserId); Parcel.PutStringGoods('FromUserName',MainForm.s_username); Parcel.PutStringGoods('MailSubject','语音'); Parcel.PutStreamGoods('MailBody',Stream); Parcel.PutStringGoods('PostDateTime',formatdatetime('yymmddhhnnss',now)); Parcel.PutStringGoods('HasRead',' '); FreeAndNil(Stream); if not mainform.MBA.AppendRecord('MailBox',Parcel) then begin FreeAndNil(Parcel); showmessage('写数据库未成功,发送失败!'+#13#10+'Error: '+MBA.LastError); exit; end; // // 向所有连接群发消息... Parcel.Clear; Parcel.PutStringGoods('MailId',MailId); Parcel.PutStringGoods('MailType','9'); MainForm.Poster.PostToWebAllSessions(Parcel); FreeANdNil(Parcel); showmessage('您的语音已经发送!'); end; except Image2.Visible := False; ShowMessage('错误:不支持录音!'); end; end;
// // 重新发送... procedure TMainForm.Button4Click(Sender: TObject); var MailId: string; Parcel: TMBParcel; Stream: TMemoryStream; begin if not fileexists(TPath.GetHomePath+PathDelim+ 'tmpsend.3GP') then begin showmessage('文件不存在,无法重发!'); exit; end; // // 生成音频文件... Stream:=TMemoryStream.Create; Stream.LoadFromFile(TPath.GetHomePath+PathDelim+ 'tmpsend.3GP'); if stringgrid2.RowCount=0 then stringgrid2.RowCount:=1; stringgrid2.Cells[0,0]:='最后发送的语音 - '+inttostr(Stream.Size)+'字节'; // // 分配Id... if not MainForm.mba.GenerateId('MailBox','MailId','SUBSTRING(MAILID,1,6)='''+formatdatetime('yymmdd',now)+'''','YYMMDDXXXX','',MailId) then begin showmessage('无法取消息号,发送失败!'); exit; end; // // 生成消息包... Parcel:=TMBParcel.Create; Parcel.PutStringGoods('MailId',MailId); Parcel.PutStringGoods('MailType','9'); Parcel.PutStringGoods('UserId',''); Parcel.PutStringGoods('UserName',''); Parcel.PutStringGoods('FromUserId',MainForm.s_UserId); Parcel.PutStringGoods('FromUserName',MainForm.s_username); Parcel.PutStringGoods('MailSubject','语音'); Parcel.PutStreamGoods('MailBody',Stream); Parcel.PutStringGoods('PostDateTime',formatdatetime('yymmddhhnnss',now)); Parcel.PutStringGoods('HasRead',' '); FreeAndNil(Stream); if not mainform.MBA.AppendRecord('MailBox',Parcel) then begin FreeAndNil(Parcel); showmessage('写数据库未成功,发送失败!'+#13#10+'Error: '+MBA.LastError); exit; end; // // 群发消息... Parcel.Clear; Parcel.PutStringGoods('MailId',MailId); Parcel.PutStringGoods('MailType','9'); MainForm.Poster.PostToWebAllSessions(Parcel); FreeANdNil(Parcel); showmessage('您的语音已经发送!'); end; // // 消息送达时,刷新消息列表... procedure TMainForm.ReceiverMessageArrives(Sender: TObject; NewMsgCount: Integer); var Parcel: TMBParcel; mailtype,mailid: string; k: integer; begin k:=0; while Receiver.PopMessage(Parcel) do begin mailtype:=parcel.GetStringGoods('MailType'); MailId:=parcel.GetStringGoods('MailId'); FreeANdNil(Parcel); if MailType='9' then begin if MBA.ReadDataset('SELECT * FROM MAILBOX WHERE MAILID='''+mailid+'''',Cds) then begin TBlobField(Cds.FieldByName('MailBody')).SaveToFile(s_defaultdir+'tmpreceived.3GP'); speaker.FileName:=s_defaultdir+'tmpreceived.3GP'; if stringgrid1.RowCount=0 then stringgrid1.RowCount:=1; stringgrid1.Cells[0,0]:='最后收到的语音 - '+inttostr(TBlobField(Cds.FieldByName('MailBody')).BlobSize)+'字节'; Cds.Close; if Assigned(Speaker.Media) then MainForm.Speaker.Play; end;
end else inc(k); end; // // 普通消息... if k>0 then begin ShowMailList; if s_soundprompt then MsgArriveSound; if s_autopopup then SendStatusbarMsg('SysPrompt','收到了['+inttostr(NewMsgCount)+']条新消息!'); end; end; // // 重播... procedure TMainForm.Button1Click(Sender: TObject); begin speaker.FileName:=s_defaultdir+'tmpreceived.3GP'; if Assigned(Speaker.Media) then MainForm.Speaker.Play; end;