tags:

views:

658

answers:

0

Hi there, I am developing a webcam chat with jmf. I got a little problem, and i didnt find the answer with google, i hope it you all will be able help to me.

So I am streaming camera's data to socket with ObjectOutputStream Code:

public class CameraTcpSocket extends Thread { private final Container con; private Socket socket; private ObjectOutputStream ooStream; private MediaPlayer mp;

/**
 * It always call from {@link CameraThread}
 * @param con
 * @param socket
 */
public CameraTcpSocket(final Container con, Socket socket) {
 mp = new MediaPlayer();
 this.con = con;
 this.socket = socket;
 try {
  ooStream = new ObjectOutputStream(socket.getOutputStream());
 } catch (IOException e) {
  e.printStackTrace();
 }

}

/**
 * Run the thread
 * Write camera's data to socket until the user is using camera
 */
@Override
public void run() {
 try {
  mp.setPlayer(con.getWcam().getPlayer());
  while(con.getWcam().isCameraIsAlive()) {
   mp.writeExternal(ooStream);
   ooStream.flush();
  }
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  if (socket != null && ooStream != null) {
   try {
    ooStream.close();
    socket.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  interrupt();
 }

}

}

And I wanted to read it : Here is the sample code:

public class ClientCam extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 3414486989373030810L;
private Socket sock;
private ObjectInputStream ins;
private JPanel jp;
private MediaPlayer mp = new MediaPlayer();
private boolean isCamUsing;

public ClientCam() {
 isCamUsing = false;
 setTitle("test");
 setLayout(null);
 setSize(320, 400);
 jp = new JPanel();
 jp.setBounds(0, 0, 310, 220);
 jp.setBorder(BorderFactory.createTitledBorder("tittle"));
 add(jp);
 setVisible(true);
 try {
  sock = new Socket("127.0.0.1", 5755);
  ins = new ObjectInputStream(sock.getInputStream());
  while (ins != null) {
   mp.readExternal(ins);
   if ( !isCamUsing ) {
    Component c = mp.getPlayer().getVisualComponent();
    c.setBounds(10, 20, 310, 220);
    jp.add(c);
    mp.getPlayer().start();
    isCamUsing = true;
   }
  }
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  if ( sock != null ) {
   try {
    sock.close();
    ins.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

}

/**
 * @param args
 */
public static void main(String[] args) {
 new ClientCam();
}

}

Then, when i connect to the server , the client got to me the next error message : java.lang.NullPointerException at javax.media.bean.playerbean.MediaPlayer.readExternal(MediaPlayer.java:1866) at main.ClientCam.(ClientCam.java:48) ( This line : mp.readExternal(ins); ) at main.ClientCam.main(ClientCam.java:76)

Is there someone who can write me an example what is working as well ? or help me ?