Hello,
I would like to stream the audio of the microphone connected with my Raspberry Pi over a Java webserver to connected webbrowsers. Unfortunately, I don't think that something like this could work:
public HTTPServer(Socket client, TargetDataLine microphone) { this.client = client; this.microphone = microphone; } public void run() { try { System.out.println("The Client " + client.getInetAddress() + ":" + client.getPort() + " is connected"); output = new DataOutputStream(client.getOutputStream()); sendResponse(); } catch (Exception e) { e.printStackTrace(); } } public void sendResponse() throws Exception { int bufferSize = microphone.getBufferSize() / 5; bufferSize += 512; String statusLine = "HTTP/1.1 200 OK\r\n"; String serverDetails = "Server: HTTP-Server\r\n"; String contentTypeLine = "Content-Type: audio/mpeg\r\n"; String contentLengthLine = "Content-Length: " + bufferSize + "\r\n"; output.writeBytes(statusLine); output.writeBytes(serverDetails); output.writeBytes(contentTypeLine); output.writeBytes(contentLengthLine); output.writeBytes("\r\n"); int numBytesRead; microphone.open(); microphone.start(); byte[] targetData = new byte[bufferSize]; while (true) { numBytesRead = microphone.read(targetData, 0, targetData.length); if (numBytesRead == -1) break; output.write(targetData, 0, numBytesRead); output.flush(); } }
ServerSocket server = null; try { server = new ServerSocket(8888); } catch (IOException e) { e.printStackTrace(); } AudioFormat format = getAudioFormat(); DataLine.Info micInfo = new DataLine.Info(TargetDataLine.class, format); TargetDataLine mic = null; try { mic = (TargetDataLine) AudioSystem.getLine(micInfo); } catch (LineUnavailableException e) { e.printStackTrace(); } System.out.println("TCPServer Waiting for client on port 8888"); while (true) { Socket connected = null; try { connected = server.accept(); } catch (IOException e) { e.printStackTrace(); } new HTTPServer(connected, mic).start(); }
Is there a library which allows something like this?