Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made a voice call application using java. the frame consists of two buttons one is to call and another is to cut the call. My problem is whenever i press the call button the threads starts running and the data(voice) is transmitted continuously but the other operations such as the cut button or frame close button doesn't respond at all. it is as if the frame hangs. can someone help me out with this?

this is my client code. haven't used any frames for server. it just runs on another machine.

Java
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import javax.sound.sampled.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Program extends JFrame implements ActionListener
  {
 JButton jbtOpen, c;
 JFrame f1=new JFrame();
 JPanel PPanel1;
 JLabel limg=new JLabel();
public final static String SERVER = JOptionPane.showInputDialog("Please enter server IP");
public Program()
{
    PPanel1 = new JPanel(null);
    PPanel1.setPreferredSize(new Dimension(1366,786));
    Container con=getContentPane();
    ImageIcon image1 = new ImageIcon("voicecall.jpg");
    ImageIcon image2 = new ImageIcon("voicecall1.jpg");
    ImageIcon image3 = new ImageIcon("call.jpg");
    jbtOpen=new JButton("Call");
    c=new JButton("Cut");
    limg.setIcon(image1);
    jbtOpen.setIcon(image2);
    c.setIcon(image3);
    limg.setBounds(0,0,1500,700);
    jbtOpen.setBounds(50,50,100,100);
    c.setBounds(200,50,100,100);
    PPanel1.add(limg);
    PPanel1.add(jbtOpen);
    PPanel1.add(c);
    setSize(400, 400);
    setVisible(true);
    setTitle("Voice Calling");
    con.add(PPanel1,BorderLayout.WEST);
    jbtOpen.addActionListener(this);
    c.addActionListener(this);
            f1.addWindowListener(new W1());
}


public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == jbtOpen)
                {
                   try {
                    open();
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                    if(e.getSource() == c){
                        System.exit(0);
                    }
                }

   }
public void open() 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.");
}

public static void main(String args[])
{
       Program b=new Program();
}
  private class W1 extends WindowAdapter
{
   public void windowClosing(WindowEvent we)
    {
    System.exit(0);
    }
}

 }
Posted

1 solution

functions belongs to call and cut wright it in separate threads,not in main threads.....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900