为来自Java servlet的音频数据提供HTML5音频控制 [英] Serving audio data from a Java servlet for an HTML5 audio control

查看:121
本文介绍了为来自Java servlet的音频数据提供HTML5音频控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个Servlets问题或HTML5问题,具体取决于解决方案的结果......:)



我有一个(Tomcat) )Servlet提供音频的短片段,然后在HTML5音频元素中拾取。音频可以正常播放,但在一些浏览器上只播放一次(因此试图倒带或重放音频不会奏效)。我怀疑这是因为我的Servlet没有报告它支持范围请求:我注意到在同一服务器上给定一个静态音频文件,Apache添加了HTTPrange-unit响应头,然后重放该文件然后工作在这样的案例。所以我认为在我遇到问题的浏览器上,为了重放文件,浏览器会发出HTTP Content-Range请求,而不是缓冲整个文件或重新请求整个文件。 (至少在Safari上,重放从我的Servlet提供的音频工作正常:我猜是因为Safari缓冲整个音频。)



所以我的问题:




  • HTML中有一种方法可以请求浏览器在播放时缓冲整个音频文件,以便允许重放而不是服务器需要必须支持范围请求?

  • 如果不是,任何人都没有任何经验来响应从Servlet的范围请求?我假设它只是(a)响应初始请求发送范围单位响应代码,然后(b)寻找相关的HTTP请求标题(内容范围?)(我猜)这是他们如何处理?)随后的请求,并只提供相关部分的音频?我应该注意哪些缺陷?

解决方案


HTML中是否有一种方法可以请求浏览器在播放时缓冲整个音频文件以允许重放而不是服务器需要支持范围请求?


即使有,也不会在所有浏览器上支持,特别是手机/ ipads /等。


有没有人有过回应来自Servlet的范围请求的经验?


有一个实施由BalusC提供。我已将它移植到我的环境中,除了一些小问题(这很可能与实现无关,但与客户端细节无关),它效果很好。


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?

解决方案

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.

这篇关于为来自Java servlet的音频数据提供HTML5音频控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆