Serving up an audio (mp3) file from a servlet

1.4k views Asked by At

So I'm serving up an mp3 file using HttpServletResponse writing to a servlet output stream.

When I go to that location in the browser, the mp3 correctly loads up and starts playing, but unfortunately, isn't able to seek anywhere(I can't scroll through the song at all)

How do I fix this, or make it so the browser downloads the full song before it starts playing.

Thanks in advance.

2

There are 2 answers

0
John Kuhns On

You need to wrap it in an audio controls tag, assuming HTML5. See http://www.w3schools.com/html/html5_audio.asp for an example. Just replace the hardcoded mp3 with a call to your servlet.

2
wero On

You servlet must be able to respond to HTTP range requests, i.e. serve not the whole mp3 from start, but also subparts.

Take a look at this thread for further information. It links to page which shows how to implement support for http range and other important headers.

EDIT: corrected URL