Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Streaming microphone audio to HTTP Client

  1. #1

    Default Streaming microphone audio to HTTP Client

    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?

  2. #2
    Junior Member
    Join Date
    Feb 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Streaming microphone audio to HTTP Client

    I'm looking for the same thing! Did you have any luck finding such a library?

Similar Threads

  1. Threads, Audio Application, Audio loading twice error
    By dpjmie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2013, 01:18 PM
  2. HTTP 1.0 Client
    By mariwan in forum Java Theory & Questions
    Replies: 1
    Last Post: October 23rd, 2012, 09:42 PM
  3. Filtering microphone sound
    By badoumba in forum Java Theory & Questions
    Replies: 0
    Last Post: April 28th, 2012, 08:02 AM
  4. http client
    By ilearn-computer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 07:47 AM
  5. Streaming Raw Data to a Client - huge message loss
    By MarkusTandy in forum Java Networking
    Replies: 0
    Last Post: February 19th, 2011, 07:41 PM