簡體   English   中英

JAVA語音通話應用

[英]JAVA app on voice calling

我正在嘗試開發有關語音呼叫的Java應用程序。 服務器端將消息中繼回連接到它的所有客戶端。 現在的問題是,即使原來發送郵件的客戶端也開始聽到他的郵件,因為服務器將郵件中繼回所有客戶端,因此我們也聽到了自己的聲音。 有什么可以幫助我的嗎?

我的服務器端代碼:

import java.net.*;
import java.io.*;
import java.util.*;

public class Echo
 {                     
       public static void main(String[] args) throws Exception

        {
             ServerSocket serverSocket = new ServerSocket(3000);
             while(true){Thread echoThread = new Thread(new EchoThread(serverSocket.accept()));
                echoThread.start();}
         }
 }

 class EchoThread implements Runnable
  {
    public static Collection<socket> sockets = new ArrayList<socket>();
    Socket connection = null;
DataInputStream dataIn = null;
DataOutputStream dataOut = null;

public EchoThread(Socket conn) throws Exception
{
    connection = conn;
    dataIn = new DataInputStream(connection.getInputStream());
    dataOut = new DataOutputStream(connection.getOutputStream());
    sockets.add(connection);
}

public void run()
{
    int bytesRead = 0;
    byte[] inBytes = new byte[1];
    while(bytesRead != -1)
    {
        try{bytesRead = dataIn.read(inBytes, 0, inBytes.length);}catch (IOException e)       {}
        if(bytesRead >= 0)
        {
            sendToAll(inBytes, bytesRead);
        }
    }
    sockets.remove(connection);
}

public static void sendToAll(byte[] byteArray, int q)
{
    Iterator<socket> sockIt = sockets.iterator();
    while(sockIt.hasNext())
    {
        Socket temp = sockIt.next();
        DataOutputStream tempOut = null;
        try
        {
            tempOut = new DataOutputStream(temp.getOutputStream());
        } catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try{tempOut.write(byteArray, 0, q);}catch (IOException e){}
      }
  }
 }

客戶端:

客戶端:這里有兩個課程。 一個從麥克風輸入,然后將其發送到服務器,另一個從服務器獲取數據,然后從揚聲器播放該數據。

import java.io.DataOutputStream;
import java.net.*;
import javax.sound.sampled.*;

 public class Program
{
  public final static String SERVER = JOptionPane.showInputDialog("Please enter server    ip");
public static void main(String[] args) throws Exception
{
    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);
    Socket conn = new Socket(SERVER,3000);
    microphone.start();
    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
    int bytesRead = 0;
    byte[] soundData = new byte[1];
    Thread inThread = new Thread(new SoundReceiver(conn));
    inThread.start();
    while(bytesRead != -1)
    {
        bytesRead = microphone.read(soundData, 0, soundData.length);
        if(bytesRead >= 0)
        {
            dos.write(soundData, 0, bytesRead);
        }
    }
    System.out.println("IT IS DONE.");
  }
}

聲音接收器類:

 import java.net.*;
 import java.io.*;

 import javax.sound.sampled.*;

 public class SoundReceiver implements Runnable
 {
 Socket connection = null;
DataInputStream soundIn = null;
SourceDataLine inSpeaker = null;

public SoundReceiver(Socket conn) throws Exception
{
    connection = conn;
    soundIn = new DataInputStream(connection.getInputStream());
    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);
}

public void run()
{
    int bytesRead = 0;
    byte[] inSound = new byte[1];
    inSpeaker.start();
    while(bytesRead != -1)
    {
        try{bytesRead = soundIn.read(inSound, 0, inSound.length);} catch (Exception e){}
        if(bytesRead >= 0)
        {
            inSpeaker.write(inSound, 0, bytesRead);
        }
      }
   }
}

看一下我們現在檢查要發送數據的套接字是否與讀取數據的套接字相同的代碼,如果是,則跳過該代碼。

public void run()
{
    int bytesRead = 0;
    byte[] inBytes = new byte[1];
    while(bytesRead != -1)
    {
        try{bytesRead = dataIn.read(inBytes, 0, inBytes.length);}catch (IOException e)       {}
        if(bytesRead >= 0)
        {
            sendToAll(connection, inBytes, bytesRead);
        }
    }
    sockets.remove(connection);
}


public static void sendToAll(Socket connection, byte[] byteArray, int q)
{
    Iterator<socket> sockIt = sockets.iterator();
    while(sockIt.hasNext())
    {
        Socket temp = sockIt.next();
        if(connection == temp){
            continue;
        }
        DataOutputStream tempOut = null;
        try
        {
            tempOut = new DataOutputStream(temp.getOutputStream());
        } catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try{tempOut.write(byteArray, 0, q);}catch (IOException e){}
      }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM