N
N
Nicholas2012-09-10 18:41:16
Java
Nicholas, 2012-09-10 18:41:16

HTML5 audio and java

Good afternoon!

I'm trying to make a proxy for a wav stream from a webcam.

The class that does this looks like this:

public class AudioHandler extends HttpServlet {


    public AudioHandler() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        response.setContentType("audio/wav");
        response.setStatus(HttpServletResponse.SC_OK);
        response.setHeader("Cache-Control", "no-cache");

        //////////////////////////////////////

        InputStream in = null;
        OutputStream out = null;

        try {

            System.err.println("[audio] Get stream!");

            // URL = http://localhost:8080/control/audio?cam=10
            URL cam = new URL("http://192.168.10." + request.getParameter("cam") + "/audio.cgi");
            URLConnection uc = cam.openConnection();
            out = new BufferedOutputStream(response.getOutputStream());

            in = uc.getInputStream();
            byte[] bytes = new byte[8192];
            int bytesRead;

            while ((bytesRead = in.read(bytes)) != -1) {
                out.write(bytes, 0, bytesRead);
            }

        } catch (IOException ex) {
            // Disconnect detected
            System.err.println("[audio " + request.getParameter("cam") + "] Audio client disconnected");
            // Прерываем поток, иначе передача не будет остановена
            Thread.currentThread().interrupt();
        }
    }
}


I learned with chagrin that the wav stream cannot be played using Flash. But HTML5 has an audio tag that can play a wav stream.

In HTML code:

<audio src="/control/audio?cam=20" controls preload="none">
                                Your browser does not support the
                                audio element.
 </audio>


I start, I hear the flow, but ... exactly 33 seconds. After that, playback stops, as if the length of the file is exactly those 33 seconds. Through Firebug, you can see that the thread did not stop.

Tell me where to dig...

Answer the question

In order to leave comments, you need to log in

8 answer(s)
N
Nicholas, 2012-09-12
@Neuronix

In general, I wrote an applet - everything works.
Who cares - github.com/Neuronix2/IRIS-X-Player

B
barker, 2012-09-10
@barker

It is not very clear from the message: so it turns out that in.read does not subtract? After 33 seconds, zero length returns all the time in the loop? It seems to me that at least it is necessary to flush the stream after write, especially since it is wrapped in BufferedOutputStream.

W
wwwsevolod, 2012-09-11
@wwwsevolod

do it via WebAudio API.
The audio tag is not intended for audio streaming apparently.

E
Emin, 2012-09-11
@Emin

Why don't OpenSource solutions for playing wav on Flash suit you?
etcs.ru/blog/as3/wav_player/stackoverflow.com/questions/668186/can-the-flash-player-play-wav-files-from-a-url
_

N
Nicholas, 2012-09-10
@Neuronix

No, the data goes further, but the player stops at 33 seconds.

N
Nicholas, 2012-09-11
@Neuronix

I tried to play in VLC - it gives the same 33 seconds. It seems that the matter is not in the player.
Stream headers look like this:

Connected to 192.168.10.20.
Escape character is '^]'.
GET /audio.cgi

HTTP/1.0 200 OK
Date: Wed Jan  6 20:20:27 2010
Server: GoAhead-Webs
Pragma: no-cache
Cache-Control: no-cache
Content-Type: audio/x-wav

RIFFWAVEfmt ▒>}data▒▒▒<▒< .... дальше идут данные

Where else can you look? What to poke? ;)

N
Nicholas, 2012-09-11
@Neuronix

And I get it like this:

Trying 192.168.10.150...
Connected to 192.168.10.150.
Escape character is '^]'.
GET /control/audio?cam=20
RIFFWAVEfmt ▒>}data▒<▒<▒<▒<

Apparently, there is a dog buried here.

N
Nicholas, 2012-09-11
@Neuronix

I just can't figure out why the headers aren't coming out... Can someone please point out the error?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question