/Users/lyon/j4p/src/sound/audioDigitizer/StreamingAudioMulticastRcvr.java
|
1 package sound.audioDigitizer;
2
3 import net.multicast.McastUtil;
4
5 import javax.sound.sampled.LineUnavailableException;
6 import java.io.IOException;
7
8 /**
9 * DocJava, Inc.
10 * http://www.docjava.com
11 * Programmer: dlyon
12 * Date: Dec 6, 2004
13 * Time: 2:37:32 PM
14 */
15 public class StreamingAudioMulticastRcvr
16 extends Thread implements Runnable {
17 public static final int PORT = 4321;
18 private McastUtil mcu = new McastUtil(PORT);
19
20 public static void main(String[] args) {
21 StreamingAudioMulticastRcvr
22 s = new StreamingAudioMulticastRcvr();
23 s.start();
24 }
25
26 private byte[] getDataGram() {
27 try {
28 return mcu.getBytes();
29 } catch (IOException e) {
30 e.printStackTrace();
31 }
32 return null;
33 }
34
35 public StreamingAudioMulticastRcvr() {
36 mcu.setLoopBack(true);
37 }
38
39 public void run() {
40 try {
41 readAndPlayLoop();
42 } catch (InterruptedException e) {
43 e.printStackTrace();
44 } catch (LineUnavailableException e) {
45 e.printStackTrace();
46 }
47 }
48
49 private void readAndPlayLoop() throws InterruptedException, LineUnavailableException {
50 byte[] b;
51 while (true) {
52 b = getDataGram();
53 if (b == null) {
54 sleep(1);
55 continue;
56 }
57 if (b.length < 0){
58 sleep(1);
59 continue;
60 }
61 PlayThread.playAudioData(b);
62 }
63 }
64 }
65