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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| package com.bass.chat;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.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;
import javax.swing.*;
public class CallGUI extends JFrame {
private static final long serialVersionUID = 1L;
// will first hold "Username:", later on "Enter message"
public JLabel label;
// to hold the Username and later on the messages
private JTextField tf;
// to hold the server address an the port number
private JTextField tfServer, tfPort;
// to Logout and get the list of the users
private JButton login, logout, whoIsIn;
// for the chat room
private JTextArea ta;
// if it is for connection
private boolean connected;
public JFrame frame;
// the Client object
private sender client;
// the default port number
private int defaultPort;
private String defaultHost;
ServerSocket MyService;
Socket clientSocket = null;
BufferedInputStream input;
TargetDataLine targetDataLine;
BufferedOutputStream out;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[10000];
public CallGUI(){
initUI();
}
public final void initUI() {
frame = new JFrame();
frame.getContentPane();
JPanel panel = new JPanel();
// getContentPane().add(panel);
JPanel northPanel = new JPanel();
// the server name anmd the port number
JPanel serverAndPort = new JPanel();
panel.setToolTipText("A Panel container");
JLabel label = new JLabel();
JButton button = new JButton(new ImageIcon("img\\Phone-icon.png"));
button.setSize(100, 100);
button.addActionListener(new BoutonListener());
JButton button2 = new JButton("allo");
northPanel.add(button2);
button.setToolTipText("Make a call");
panel.add(button);
panel.add(label);
// frame.setLayout(new GridLayout(1, 2));
frame.add(panel,BorderLayout.SOUTH);
frame.add(northPanel);
frame.setTitle("Tooltip");
frame.setSize(600, 600);
frame.setVisible(true);
panel.setBackground(Color.DARK_GRAY);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
class BoutonListener implements ActionListener {
//Red�finition de la m�thode actionPerformed()
public void actionPerformed(ActionEvent arg0) {
try {
sender();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void 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(1500);
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) {
e.printStackTrace();
}
}
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);
}
}
class CaptureThread extends Thread {
byte tempBuffer[] = new byte[10000];
public void run() {
try {
while (true) {
int cnt = targetDataLine.read(tempBuffer, 0,
tempBuffer.length);
out.write(tempBuffer);
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
} |
Partager