开发者

Playing audio file in jsp

1) Is there a way in java to stream an audio file from ftp on my jsp page?

2) How can I play an audio file in jsp without audio file being download on client browser (like live stream) - temporary internet files

pl开发者_如何学编程ease help with some code example


Streaming audio from a file does not have anything to do with the JSP itself.

You want to embed your audio file into the page using HTML tags. You can do this directly like:

<embed src="audiofile.mp3">

Or build something a bit more fancy using a Flash movie as an audio player, or find a JavaScript library. Here is a page on how to do it with various methods.

The JSP can provide the audio file name and perhaps other options, but that's all it has to do -- the rest is handled by plain old HTML tags.

You can find more information by searching the Web for the terms "embed" and "audio".


If you're already on HTML5, just use <audio> element, See also the HTML Dog tutorial.

<audio src="file.wav" controls="true" />

If you're not on HTML5 yet, audio in a webbpage is usually to be included by the HTML <embed> element (which uses the platform default player) or the HTML <object> element (wherein you can specify a more specific player for which the webbrowser can eventually automagically download the necessary plugin software).

<object data="file.wav" />

Either way, it should point to an URL which returns the audio stream. This can be just a static file in the public webcontent, next to the JSP file. E.g. http://example.com/context/file.wav. But when the audio file is stored outside the public webcontent or in a database, then you'd like to stream it via a Servlet. Basically just get an InputStream of it (using e.g. FileInputStream) and then write it to the OutputStream of the response along a correct set of response headers.

Noted should be that JSP is irrelevant in this particular question. It's just a view technology providing a template to write HTML in and send it to the webbrowser.


That said, websites which plays audio are generally considered annoying and generally scare off visitors. Keep this in mind if you consider User Experience important.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜