语音通信的问题,有代码大家帮我看看,急 ( 积分: 200 )

  • 主题发起人 主题发起人 小飞龙
  • 开始时间 开始时间

小飞龙

Unregistered / Unconfirmed
GUEST, unregistred user!
Client的代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ScktComp, ACMWaveIn, ACMDialog, ACMWaveOut,Unit2;
const
AudioFormatGSM8K: array[1..50] of byte=($31, $00, $01, $00, $40, $1F, $00, $00, $59, $06, $00,
$00, $41, $00,$00, $00 , $02, $00, $40, $01, $00, $00, $00, $00, $00, $00, $00, $00,$00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00, $00, $74, $B4, $44, $00, $F8, $6C, $CD, $00, $44, $10);
const
MYMSG =WM_USER+100;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
RichEdit1: TRichEdit;
Button1: TButton;
Button2: TButton;
StatusBar1: TStatusBar;
ACMWaveOut1: TACMWaveOut;
ACMWaveIn1: TACMWaveIn;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ACMWaveIn1Data(data: Pointer; size: Integer);
procedure FormCreate(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
procedure MESDO(var Msg:TMessage);message MYMSG;
end;

var
Form1: TForm1;
bs :bool;
cc:integer;
mmsg:GetBuf;
implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
clientsocket1.Active :=false;
clientsocket1.close;
mmsg.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
//var str1:string;
begin
//str1:=inputbox('建立连接','请输入IP','192.168.0.25');
//if trim(str1)<>'' then
//begin
clientsocket1.Port:=5555;//server port
clientsocket1.host:='192.168.0.25';
try
clientsocket1.active:=true;
except
showmessage('连接失败');
end;
//end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText:='连接成功';
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
mdata:array[1..40000] of char;
data:Pchar;
begin
inc(cc);
if bs then 这里进不去
Socket.ReceiveBuf(mdata,Socket.ReceiveLength);
data:=pchar(dword(@mdata));
ACMWaveOut1.PlayBack(data,Socket.ReceiveLength);
RichEdit1.Lines.Add(inttostr(cc));
if bs=false then
begin
SendMessage(handle,MYMSG,0,0);
bs:=true;
end;
end;

procedure TForm1.ACMWaveIn1Data(data: Pointer; size: Integer);
var
mdata:array[1..40000] of char;
begin
if Size<=0 then exit;
if bs=true then
begin
move(data^,mdata,size);
ClientSocket1.Socket.Sendbuf(mdata,size);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
bs:=false;
cc:=0;
end;

procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode:=0;
end;
procedure TForm1.MESDO(var Msg:TMessage);
begin
acmwaveout1.Open(@AudioFormatGSM8K);
acmwavein1.Open(@AudioFormatGSM8K);
StatusBar1.SimpleText:='可以进行语音聊天了';
bs:=true;
end;
end.

Server的代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ComCtrls;
const
AudioFormatGSM8K: array[1..50] of byte=($31, $00, $01, $00, $40, $1F, $00, $00, $59, $06, $00,
$00, $41, $00,$00, $00 , $02, $00, $40, $01, $00, $00, $00, $00, $00, $00, $00, $00,$00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00, $00, $74, $B4, $44, $00, $F8, $6C, $CD, $00, $44, $10);
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
RichEdit1: TRichEdit;
StatusBar1: TStatusBar;
GroupBox2: TGroupBox;
ListView1: TListView;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
count:integer;
tran :bool;
cc:integer;
IP1,IP2:string;
port1,port2:integer;
c1,c2:integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
serversocket1.Active :=true;
StatusBar1.SimpleText:='聊天室服务器启动了';
cc:=0;
c1:=0;
c2:=0;
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
mdata:array[1..40000] of char;
mdata1:array[1..40000] of char;
begin
if tran then
begin
inc(cc);
RichEdit1.Lines.Add(inttostr(Socket.ReceiveLength));
StatusBar1.SimpleText:=inttostr(cc);

if Socket.RemoteAddress=ip2 then
begin
Socket.ReceiveBuf(mdata,Socket.ReceiveLength);
ServerSocket1.Socket.Connections[0].SendBuf(mdata,Socket.ReceiveLength);
label1.Caption:=inttostr(c1);
inc(c1);
end
else if Socket.RemoteAddress=ip1 then
begin
Socket.ReceiveBuf(mdata1,Socket.ReceiveLength);
ServerSocket1.Socket.Connections[1].SendBuf(mdata1,Socket.ReceiveLength);
label2.Caption:=inttostr(c2);
inc(c2);
end;
end;
end;


procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
//var
// i:integer;
begin
dec(count);
if count<2 then
Tran :=false;
// for i:=0 to count do
// ServerSocket1.Socket.Connections.SendText('close');
end;

procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i:integer;
begin

if count=0 then
begin
ip1:=socket.RemoteAddress;
port1:=socket.RemotePort;
//inc(count);
end;
if count=1 then
begin
ip2:=socket.RemoteAddress;
port2:=socket.RemotePort;
//
end;
inc(count);
if count=2 then
begin
for i:=0 to count-1 do
ServerSocket1.Socket.Connections.SendText('start');
tran:=true;
end
end;
end.

在收到server通知后打开acm控件,但是这样bs虽然为真,但是在OnRead里面根本就进不去,好象OnRead不执行,在Server那里用单步执行,看到有执行发送,什么问题啊?
 
Client的代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ScktComp, ACMWaveIn, ACMDialog, ACMWaveOut,Unit2;
const
AudioFormatGSM8K: array[1..50] of byte=($31, $00, $01, $00, $40, $1F, $00, $00, $59, $06, $00,
$00, $41, $00,$00, $00 , $02, $00, $40, $01, $00, $00, $00, $00, $00, $00, $00, $00,$00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00, $00, $74, $B4, $44, $00, $F8, $6C, $CD, $00, $44, $10);
const
MYMSG =WM_USER+100;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
RichEdit1: TRichEdit;
Button1: TButton;
Button2: TButton;
StatusBar1: TStatusBar;
ACMWaveOut1: TACMWaveOut;
ACMWaveIn1: TACMWaveIn;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ACMWaveIn1Data(data: Pointer; size: Integer);
procedure FormCreate(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
procedure MESDO(var Msg:TMessage);message MYMSG;
end;

var
Form1: TForm1;
bs :bool;
cc:integer;
mmsg:GetBuf;
implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
clientsocket1.Active :=false;
clientsocket1.close;
mmsg.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
//var str1:string;
begin
//str1:=inputbox('建立连接','请输入IP','192.168.0.25');
//if trim(str1)<>'' then
//begin
clientsocket1.Port:=5555;//server port
clientsocket1.host:='192.168.0.25';
try
clientsocket1.active:=true;
except
showmessage('连接失败');
end;
//end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText:='连接成功';
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
mdata:array[1..40000] of char;
data:Pchar;
begin
inc(cc);
if bs then 这里进不去
Socket.ReceiveBuf(mdata,Socket.ReceiveLength);
data:=pchar(dword(@mdata));
ACMWaveOut1.PlayBack(data,Socket.ReceiveLength);
RichEdit1.Lines.Add(inttostr(cc));
if bs=false then
begin
SendMessage(handle,MYMSG,0,0);
bs:=true;
end;
end;

procedure TForm1.ACMWaveIn1Data(data: Pointer; size: Integer);
var
mdata:array[1..40000] of char;
begin
if Size<=0 then exit;
if bs=true then
begin
move(data^,mdata,size);
ClientSocket1.Socket.Sendbuf(mdata,size);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
bs:=false;
cc:=0;
end;

procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode:=0;
end;
procedure TForm1.MESDO(var Msg:TMessage);
begin
acmwaveout1.Open(@AudioFormatGSM8K);
acmwavein1.Open(@AudioFormatGSM8K);
StatusBar1.SimpleText:='可以进行语音聊天了';
bs:=true;
end;
end.

Server的代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ComCtrls;
const
AudioFormatGSM8K: array[1..50] of byte=($31, $00, $01, $00, $40, $1F, $00, $00, $59, $06, $00,
$00, $41, $00,$00, $00 , $02, $00, $40, $01, $00, $00, $00, $00, $00, $00, $00, $00,$00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00, $00, $74, $B4, $44, $00, $F8, $6C, $CD, $00, $44, $10);
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
RichEdit1: TRichEdit;
StatusBar1: TStatusBar;
GroupBox2: TGroupBox;
ListView1: TListView;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
count:integer;
tran :bool;
cc:integer;
IP1,IP2:string;
port1,port2:integer;
c1,c2:integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
serversocket1.Active :=true;
StatusBar1.SimpleText:='聊天室服务器启动了';
cc:=0;
c1:=0;
c2:=0;
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
mdata:array[1..40000] of char;
mdata1:array[1..40000] of char;
begin
if tran then
begin
inc(cc);
RichEdit1.Lines.Add(inttostr(Socket.ReceiveLength));
StatusBar1.SimpleText:=inttostr(cc);

if Socket.RemoteAddress=ip2 then
begin
Socket.ReceiveBuf(mdata,Socket.ReceiveLength);
ServerSocket1.Socket.Connections[0].SendBuf(mdata,Socket.ReceiveLength);
label1.Caption:=inttostr(c1);
inc(c1);
end
else if Socket.RemoteAddress=ip1 then
begin
Socket.ReceiveBuf(mdata1,Socket.ReceiveLength);
ServerSocket1.Socket.Connections[1].SendBuf(mdata1,Socket.ReceiveLength);
label2.Caption:=inttostr(c2);
inc(c2);
end;
end;
end;


procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
//var
// i:integer;
begin
dec(count);
if count<2 then
Tran :=false;
// for i:=0 to count do
// ServerSocket1.Socket.Connections.SendText('close');
end;

procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i:integer;
begin

if count=0 then
begin
ip1:=socket.RemoteAddress;
port1:=socket.RemotePort;
//inc(count);
end;
if count=1 then
begin
ip2:=socket.RemoteAddress;
port2:=socket.RemotePort;
//
end;
inc(count);
if count=2 then
begin
for i:=0 to count-1 do
ServerSocket1.Socket.Connections.SendText('start');
tran:=true;
end
end;
end.

在收到server通知后打开acm控件,但是这样bs虽然为真,但是在OnRead里面根本就进不去,好象OnRead不执行,在Server那里用单步执行,看到有执行发送,什么问题啊?
 
自己顶一下
 
我再顶上去,高手快来啊
 
一起研究啊,412518736
 
Socket.ReceiveLength 不能直接用!
int iLen := Socket.ReceiveBuf(mdata,Socket.ReceiveLength);
ServerSocket1.Socket.Connections[0].SendBuf(mdata,ilen);
 
不行啊,还是一样
 
那你Server有问题,没有执行发送啊
 
用udp控件吧,一起研究啊,qq:412518736
 
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
mdata:array[1..40000] of char;
mdata1:array[1..40000] of char;
begin
if tran then
begin
inc(cc);
RichEdit1.Lines.Add(inttostr(Socket.ReceiveLength));
StatusBar1.SimpleText:=inttostr(cc);

if Socket.RemoteAddress=ip2 then
begin
[red] Socket.ReceiveBuf(mdata,Socket.ReceiveLength);
ServerSocket1.Socket.Connections[0].SendBuf(mdata,Socket.ReceiveLength);[/red] 这两行有问题,Socket数据一旦读取ReceiveLength值就不是原来的了。
label1.Caption:=inttostr(c1);
inc(c1);
end
else if Socket.RemoteAddress=ip1 then
begin
[red] Socket.ReceiveBuf(mdata1,Socket.ReceiveLength);
ServerSocket1.Socket.Connections[1].SendBuf(mdata1,Socket.ReceiveLength);[/red][gold]这两行有问题,Socket数据一旦读取ReceiveLength值就不是原来的了。[/gold]
label2.Caption:=inttostr(c2);
inc(c2);
end;
end;
end;
 
abookdog:这个我已经该过了,但是还是不行啊
我用udp做过也是一样的问题
 
最近tom.com的skpe语音传输,效果最好,不知道能否达到此效果
 
自己再顶上去,
 
再顶上去
 
再顶上去
 
凄惨死,
 

Similar threads

后退
顶部