我正在尝试使用 java multiCastSocket 编程来实现语音聊天。从麦克风获取输入并尝试在扬声器上聆听。问题是我无法听到任何声音。我通过它进行了wireshark调试,我可以看到我能够将数据包发送到多播组地址,并且我可以看到加入消息,但是没有从多播组地址返回的数据包。任何人都可以看看代码,如果我做错了什么,请告诉我?提前致谢。
/发件人代码/
公共类 MulticastAudioSender {
public static final int IPM_PORT = 7778;
public static final String IPM_ADDRESS = "235.1.1.1";
public static final int SLEEP_MILLISECS = 1000;
private MulticastSocket sock;
private DatagramPacket sendPack;
public static final int BUFFER_SIZE = 20000;
public MulticastAudioSender(){
try {
sock = new MulticastSocket();
} catch( Exception e ) {
System.out.println( "Exception in creating socket or packet: "
+ e.getMessage() );
}
}
private void sendMessage(byte[] soundData, int length) {
try {
sendPack = new DatagramPacket( soundData, length,
InetAddress.getByName( IPM_ADDRESS ), IPM_PORT );
sock.send( sendPack );
//System.out.println( "Message sent." );
} catch( Exception e ) {
System.out.println( "Exception in sending packet: "
+ e.getMessage() );
}
}
public static void main( String[] args ) throws Exception{
MulticastAudioSender sender = new MulticastAudioSender();
AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, af);
TargetDataLine microphone = (TargetDataLine)AudioSystem.getLine(info);
microphone.open(af);
microphone.start();
int bytesRead = 0;
byte[] soundData = new byte[1];
//sender.receiveMessage();
while(bytesRead != -1) {
bytesRead = microphone.read(soundData, 0, soundData.length);
if(bytesRead >= 0){
sender.sendMessage(soundData, soundData.length);
}
}
}
}
/收货人代码/
公共类 IPMulticastReceiver {
public static final int IPM_PORT = 7778;
public static final String IPM_ADDRESS = "235.1.1.1";
public static final int BUFFER_SIZE = 200;
private MulticastSocket sock;
private DatagramPacket pack;
private byte[] receiveBuffer;
byte[] inSound;
SourceDataLine inSpeaker = null;
public IPMulticastReceiver(){
try {
sock = new MulticastSocket( IPM_PORT );
sock.joinGroup( InetAddress.getByName( IPM_ADDRESS ) );
AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
inSpeaker = (SourceDataLine)AudioSystem.getLine(info);
inSpeaker.open(af);
} catch( Exception e ) {
System.out.println( "Exception in creating Socket or Packet: "
+ e.getMessage() );
}
}
private void receiveMessage() {
try {
receiveBuffer = new byte[BUFFER_SIZE];
//sock.joinGroup(InetAddress.getByName(IPM_ADDRESS));
pack = new DatagramPacket( receiveBuffer, receiveBuffer.length );
sock.receive( pack );
/*Thread inThread = new Thread(new MultiCastSoundReceiver(receiveBuffer));
inThread.start();*/
inSpeaker.write(receiveBuffer, 0, receiveBuffer.length);
inSpeaker.drain();
} catch( Exception e ) {
e.printStackTrace();
System.out.println( "Exception in receiving packet: "
+ e.getMessage() );
}
}
public static void main( String[] args ) throws LineUnavailableException {
IPMulticastReceiver receiver = new IPMulticastReceiver();
while( true ) {
receiver.receiveMessage();
}
}
}