tags:

views:

162

answers:

0

hi, i have a client/server application with java rmi, i want it to transfer voice between cllients, but i dont know how! i have written this classes:

public class MicReader extends Thread implements Serializable
{
    private static final long serialVersionUID = 7170035360785238031L;
    private TargetDataLine line;
    private boolean isOver = false;
    public static final int MAXBUFLENGHT = 1024; 

    /**
     * 
     *
     */
    public MicReader ()
    {
        try
        {
            AudioFormat aFormat = new AudioFormat (
                    AudioFormat.Encoding.PCM_SIGNED ,
                    44100.0f ,
                    16 , 2 , 4 ,
                    44100.0f , false );

            DataLine.Info info = new DataLine.Info ( TargetDataLine.class ,aFormat );
            line = (TargetDataLine) AudioSystem.getLine ( info );
            line.open ( aFormat );


        }
        catch ( Exception e )
        {
            e.printStackTrace ();
        }
    }

    /**
     * 
     *
     */
    public void startRecording ()
    {
        line.start ();
        this.start ();
    }

    public void stopRecording ()
    {
         isOver = true;
    }

    public void run ()
    {
        byte[] data = new byte[ MAXBUFLENGHT ];

        while ( ! isOver )
        {
            try
            {
                line.read ( data , 0 , MAXBUFLENGHT );
                DatagramPacket pack = new DatagramPacket( data, data.length );

                MyClient.is.send ( data,data.length );
            }
            catch ( Exception e1 )
            {
                e1.printStackTrace ();
            }
        }
    }


    /**
     * @return the isOver
     */
    public boolean isOver ()
    {
        return isOver;
    }

    /**
     * @param isOver the isOver to set
     */
    public void setOver ( boolean isOver )
    {
        this.isOver = isOver;
    }
}

--------

public class AudioPlayer
{
    private SourceDataLine line;

    public AudioPlayer ()
    {
        try
        {
            AudioFormat aFormat = new AudioFormat (
                    AudioFormat.Encoding.PCM_SIGNED , 44100.0f , 16 , 2 , 4 ,
                    44100.0f , false );
            DataLine.Info info = new DataLine.Info ( SourceDataLine.class ,
                    aFormat );
            line = (SourceDataLine) AudioSystem.getLine ( info );

            line.open ( aFormat );
        }
        catch ( Exception e )
        {
            e.printStackTrace ();
        }
    }

    /**
     * This function plays the audio
     * @param data
     */
    public void playFile ( byte[] data )
    {
        try
        {
            if ( data.equals ( null ) )
            {
                return;
            }
            else
            {
                line.start ();
                line.write ( data , 0 , 1024 );
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace ();
        }
    }
}
------------

Client


public class MyClient extends JFrame implements IClient {
    /**
     * 
     * 
     */
    private static boolean muted = true;

    public static boolean isFinished = true;

    private static final long serialVersionUID = 1L;

    public JButton talkButton;

    private TargetDataLine line;

    private boolean isOver = false;

    static IServer is;

    public static final int MAXBUFLENGHT = 1024;

    /**
     * Launch the application
     * 
     * @param args
     */
    public static void main(String args[]) {
        try {
            is = (IServer) Naming.lookup("Server");
            MyClient frame = new MyClient();
            is.add(frame);
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the frame
     */
    public MyClient() throws RemoteException {
        super();
        getContentPane().setLayout(null);
        UnicastRemoteObject.exportObject(this);
        setBounds(100, 100, 361, 248);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        talkButton = new JButton();
        talkButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent arg0) {
                MicReader mic=new MicReader();
                mic.start();

            }
        });

        talkButton.setText("Talk");
        talkButton.setBounds(112, 76, 100, 35);
        getContentPane().add(talkButton);

    }



    public void recieve(byte[] data, int length) throws RemoteException {
        while(true)
        {
            AudioPlayer a = new AudioPlayer();
            byte[] tempData = new byte[ MAXBUFLENGHT ];
            tempData=data;
            a.playFile(tempData);
        }
    }
}

------------

Server:


public class Server extends JFrame implements IServer {
    private ArrayList<IClient> clients = new ArrayList<IClient>();

    /**
     * Launch the application
     * 
     * @param args
     */
    public static void main(String args[]) {
        try {
            Server frame = new Server();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the frame
     */
    public Server() throws RemoteException {
        super();
        try {
            UnicastRemoteObject.exportObject(this);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        getContentPane().setLayout(null);
        setBounds(100, 100, 500, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JLabel serverLabel = new JLabel();
        serverLabel.setFont(new Font("", Font.PLAIN, 16));
        serverLabel.setText("Server");
        serverLabel.setBounds(94, 41, 74, 35);
        getContentPane().add(serverLabel);
        //
    }



    public void add(IClient frame) throws RemoteException {
        clients.add(frame);

    }



    public void send(byte[] data, int length) throws RemoteException {
        // TODO Auto-generated method stub
        for (IClient iClient : clients) {
            iClient.recieve(data,length);
        }
    }

}

and there is another class Main. what can i change the myClient methods to solve the probem? thanks