Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

html - Serving audio data from a Java servlet for an HTML5 audio control

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...