語音聊天的程式

转自:http://www.javaworld.com.tw/jute/post/view?bid=35&id=208615&sty=1&tpg=1&age=
這是一份已經寫好的語音聊天的程式希望可以分享給大家

server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
 
import javax.sound.sampled.AudioFormat;
//import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
 
public class sender {
  
  ServerSocket MyService;
  Socket clientSocket = null;
  BufferedInputStream input;
  TargetDataLine targetDataLine;
 
  BufferedOutputStream out;
  ByteArrayOutputStream byteArrayOutputStream;
  AudioFormat audioFormat;  
  
  SourceDataLine sourceDataLine;  
  byte tempBuffer[] = new byte[10000];
  
   sender() throws LineUnavailableException{
  try {
    audioFormat = getAudioFormat();
    DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class,audioFormat);
    sourceDataLine = (SourceDataLine)
   AudioSystem.getLine(dataLineInfo);
   sourceDataLine.open(audioFormat);
   sourceDataLine.start();
      MyService = new ServerSocket(500);
      clientSocket = MyService.accept();
      captureAudio();
      input = new BufferedInputStream(clientSocket.getInputStream());  
      out=new BufferedOutputStream(clientSocket.getOutputStream());
      
      while(input.read(tempBuffer)!=-1){  
        
        sourceDataLine.write(tempBuffer,0,10000);
      }
    } catch (IOException e) {
      
      System.out.print(e.toString());
    }

  }
   private AudioFormat getAudioFormat(){
     float sampleRate = 8000.0F;    
     int sampleSizeInBits = 16;    
     int channels = 1;    
     boolean signed = true;    
     boolean bigEndian = false;    
     return new AudioFormat(
     sampleRate,
     sampleSizeInBits,
     channels,
     signed,
     bigEndian);
     }
  
  
  
  private void captureAudio() {
    try {
      
      Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
      System.out.println("Available mixers:");
      for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
        System.out.println(mixerInfo[cnt].getName());
      }
      audioFormat = getAudioFormat();
 
      DataLine.Info dataLineInfo = new DataLine.Info(
          TargetDataLine.class, audioFormat);
 
      Mixer mixer = AudioSystem.getMixer(mixerInfo[3]);
 
      targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
 
      targetDataLine.open(audioFormat);
      targetDataLine.start();
 
      Thread captureThread = new CaptureThread();
      captureThread.start();    
    } catch (Exception e) {
      System.out.println(e);
      System.exit(0);
    }
  }
  public static void main(String s[]) throws LineUnavailableException{
    
    try{
  
       System.out.print("Server starting ");
       //sender s2 = new sender();
       new sender();
    
     }catch(Exception e){
      e.toString();
     }
    
  }
  
  
  
  
  class CaptureThread extends Thread {
 
    byte tempBuffer[] = new byte[10000];
 
    public void run() {      
      try {
        while (true) {
          /*
          int cnt;
          cnt = targetDataLine.read(tempBuffer, 0,
              tempBuffer.length);
          out.write(tempBuffer);
          */

          
          @SuppressWarnings("unused") int cnt = targetDataLine.read(tempBuffer, 0,
              tempBuffer.length);
          out.write(tempBuffer);
        }
        
      } catch (Exception e) {
        System.out.println(e);
        System.exit(0);
      }
    }
  }
 
}
 


client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
 
import javax.sound.sampled.TargetDataLine;
 
public class Tx {
  
  boolean stopCapture = false;
 
  ByteArrayOutputStream byteArrayOutputStream;
 
  AudioFormat audioFormat;
 
  TargetDataLine targetDataLine;
 
  AudioInputStream audioInputStream;
 
  BufferedOutputStream out = null;
 
  BufferedInputStream in = null;
 
  Socket sock = null;
 
  SourceDataLine sourceDataLine;
 
  public static void main(String[] args) {
    Tx tx = new Tx();
    tx.captureAudio();
 
  }
 
  private void captureAudio() {
    try {
 
      sock = new Socket("localhost", 500);
 
      out = new BufferedOutputStream(sock.getOutputStream());
      in = new BufferedInputStream(sock.getInputStream());
 
      Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
      System.out.println("Available mixers:");
      for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
        System.out.println(mixerInfo[cnt].getName());
      }
      audioFormat = getAudioFormat();
 
      DataLine.Info dataLineInfo = new DataLine.Info(
          TargetDataLine.class, audioFormat);
 
      Mixer mixer = AudioSystem.getMixer(mixerInfo[3]);
 
      targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
 
      targetDataLine.open(audioFormat);
      targetDataLine.start();
 
      Thread captureThread = new CaptureThread();
      captureThread.start();
 
      DataLine.Info dataLineInfo1 = new DataLine.Info(
          SourceDataLine.class, audioFormat);
      sourceDataLine = (SourceDataLine) AudioSystem
          .getLine(dataLineInfo1);
      sourceDataLine.open(audioFormat);
      sourceDataLine.start();
 
      Thread playThread = new PlayThread();
      playThread.start();
 
    } catch (Exception e) {
      System.out.println(e);
      System.exit(0);
    }
  }
 
  class CaptureThread extends Thread {
 
    byte tempBuffer[] = new byte[10000];
 
    public void run() {
      byteArrayOutputStream = new ByteArrayOutputStream();
      stopCapture = false;
      try {
        while (!stopCapture) {
 
          int cnt = targetDataLine.read(tempBuffer, 0,
              tempBuffer.length);
 
          out.write(tempBuffer);
 
          if (cnt > 0) {
 
            byteArrayOutputStream.write(tempBuffer, 0, cnt);
 
          }
        }
        byteArrayOutputStream.close();
      } catch (Exception e) {
        System.out.println(e);
        System.exit(0);
      }
    }
  }
 
  private AudioFormat getAudioFormat() {
    float sampleRate = 8000.0F;
 
    int sampleSizeInBits = 16;
 
    int channels = 1;
 
    boolean signed = true;
 
    boolean bigEndian = false;
 
    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
        bigEndian);
  }
 
  class PlayThread extends Thread {
    byte tempBuffer[] = new byte[10000];
 
    public void run() {
      try {
        while (in.read(tempBuffer) != -1) {
          sourceDataLine.write(tempBuffer, 0, 10000);
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
 
}
 



posted on 2009-04-09 23:27 .VwV. 阅读(375) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜