Serving audio data from a Java servlet for an HTML

2019-01-28 15:03发布

This may be a Servlets question or an HTML5 question, depending on what the solution turns out to be... :)

I've got a (Tomcat) Servlet serving up short clips of audio which are then picked up in an HTML5 audio element. The audio correctly plays, but on some browsers only once (so that attempting to "rewind" or replay the audio does not then work). I suspect that this is because my Servlet is not reporting that it supports range requests: I notice that given a static audio file on the same server, Apache adds the HTTP "range-unit" response header, and replaying the file then works in such cases. So I am supposing that on browsers where I'm having the issue, in order to replay the file, the browser makes an HTTP Content-Range request rather than either buffering the entire file or re-requesting the entire file. (On Safari at least, replaying the audio served from my Servlet works fine: I'm guessing because Safari buffers the entire audio.)

So my questions:

  • is there a way in HTML to request that the browser buffer the entire audio file on playing in order to allow replays rather than the server needing necessarily to support range requests?

  • if not, does anyone have any experience of responding to range requests from a Servlet? I'm assuming it's just a case of (a) sending a "range-unit" response code in response to the initial request, then (b) looking out for relevant HTTP request headers ("Content-Range?") (I guess that's how they're handled?) on subsequent requests and only serving the relevant portion of the audio? Are there any pitfalls I should be aware of?

1条回答
beautiful°
2楼-- · 2019-01-28 15:20

is there a way in HTML to request that the browser buffer the entire audio file on playing in order to allow replays rather than the server needing necessarily to support range requests?

Even if there is, it will not be supported on all browsers, especially mobiles/ipads/etc.

does anyone have any experience of responding to range requests from a Servlet?

There's implementation provided by BalusC. I have ported it to my environment and, apart some minor issues, (which are most likely unrelated to the implementation but to the client side specifics) it works great.

查看更多
登录 后发表回答