zgillis

Sound Client: SoundReceiver.java

Jul 23rd, 2012
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. import javax.sound.sampled.*;
  5.  
  6. public class SoundReceiver implements Runnable
  7. {
  8.     Socket connection = null;
  9.     DataInputStream soundIn = null;
  10.     SourceDataLine inSpeaker = null;
  11.    
  12.     public SoundReceiver(Socket conn) throws Exception
  13.     {
  14.         connection = conn;
  15.         soundIn = new DataInputStream(connection.getInputStream());
  16.         AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
  17.         DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
  18.         inSpeaker = (SourceDataLine)AudioSystem.getLine(info);
  19.         inSpeaker.open(af);
  20.     }
  21.    
  22.     public void run()
  23.     {
  24.         int bytesRead = 0;
  25.         byte[] inSound = new byte[1];
  26.         inSpeaker.start();
  27.         while(bytesRead != -1)
  28.         {
  29.             try{bytesRead = soundIn.read(inSound, 0, inSound.length);} catch (Exception e){}
  30.             if(bytesRead >= 0)
  31.             {
  32.                 inSpeaker.write(inSound, 0, bytesRead);
  33.             }
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment