mr = new MicrophoneReader(); mr.start(); ServerSocket ss = new ServerSocket(7373); while (true) { Socket s = ss.accept(); Sender sndr = new Sender(s); senderList.add(sndr); sndr.start(); }
public void run() { try { microphone = AudioSystem.getTargetDataLine(format); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); microphone = (TargetDataLine) AudioSystem.getLine(info); microphone.open(format); data = new byte[CHUNK_SIZE]; microphone.start(); while (!finishFlag) { synchronized (monitor) { if (senderNotReady==sendersCreated) { monitor.notifyAll(); continue; } numBytesRead = microphone.read(data, 0, CHUNK_SIZE); } System.out.print("Microphone reader: "); System.out.print(numBytesRead); System.out.println(" bytes read"); } } catch (LineUnavailableException e) { e.printStackTrace(); } }
public void run() { try { OutputStream os = s.getOutputStream(); while (!finishFlag) { synchronized (monitor) { senderNotReady++; monitor.wait(); os.write(data, 0, numBytesRead); os.flush(); senderNotReady--; } System.out.print("Sender #"); System.out.print(senderNumber); System.out.print(": "); System.out.print(numBytesRead); System.out.println(" bytes sent"); } } catch (Exception e) { e.printStackTrace(); } }
try { InetAddress ipAddr = InetAddress.getByName(host); Socket s = new Socket(ipAddr, 7373); InputStream is = s.getInputStream(); DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format); speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo); speakers.open(format); speakers.start(); Scanner sc = new Scanner(System.in); int numBytesRead; byte[] data = new byte[204800]; while (true) { numBytesRead = is.read(data); speakers.write(data, 0, numBytesRead); } } catch (Exception e) { e.printStackTrace(); }
toogle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!isRunning) { isRunning = true; toogle.setText("Stop"); rp = new ReceiverPlayer(hostname.getText().toString()); rp.start(); } else { toogle.setText("Start"); isRunning = false; rp.setFinishFlag(); } } });
class ReceiverPlayer extends Thread { volatile boolean finishFlag; String host; public ReceiverPlayer(String hostname) { host = hostname; finishFlag = false; } public void setFinishFlag() { finishFlag = true; } public void run() { try { InetAddress ipAddr = InetAddress.getByName(host); Socket s = new Socket(ipAddr, 7373); InputStream is = s.getInputStream(); int bufferSize = AudioTrack.getMinBufferSize(16000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT); int numBytesRead; byte[] data = new byte[bufferSize]; AudioTrack aTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 16000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM); aTrack.play(); while (!finishFlag) { numBytesRead = is.read(data, 0, bufferSize); aTrack.write(data, 0, numBytesRead); } aTrack.stop(); s.close(); } catch (Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.e("Error",sw.toString()); } } }
// Java SE: AudioFormat format = new AudioFormat(16000.0f, 16, 2, true, bigEndian); // Android: AudioTrack aTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 16000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
case "$1" in start) java -avian -jar jAudioReceiver.jar 192.168.1.50 & echo "kill -KILL $!">kill_receiver.sh ;; stop) ./kill_receiver.sh ;; esac
Source: https://habr.com/ru/post/242949/