public class MicrophoneStreaming {
private static final AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
private static Logger logger = LoggerFactory.getLogger(MicrophoneStreaming.class);
static class MyHandler implements HttpHandler {
public void handle(HttpExchange httpExchange) throws IOException {
TargetDataLine microphone;
try {
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
microphone = (TargetDataLine) AudioSystem.getLine(info);
microphone.open(format);
int[] numBytesRead = new int[1];
int CHUNK_SIZE = 1024;
byte[] data = new byte[microphone.getBufferSize() / 5];
microphone.start();
int[] bytesRead = {0};
httpExchange.sendResponseHeaders(200, bytesRead[0]);
DataOutputStream dataOutputStream = new DataOutputStream(httpExchange.getResponseBody());
while (true) {
numBytesRead[0] = microphone.read(data, 0, CHUNK_SIZE);
bytesRead[0] += numBytesRead[0];
dataOutputStream.write(data, 0, numBytesRead[0]);
}
} catch (Exception e) {
logger.error("Line Error", e);
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/test", new MicrophoneStreaming.MyHandler());
server.setExecutor(null);
server.start();
}
}
Aucun commentaire:
Enregistrer un commentaire