[ Team LiB ] Previous Section Next Section

Recipe 17.8 Embedding a Background Soundtrack in a JSP

Problem

You want to embed an audio file in your JSP.

Solution

Use the embed tag in the JSP. Use the hidden attribute if you want to hide the audio controls; otherwise, specify a width and height attribute for showing the audio controls.

Discussion

The embed tag is used to include an audio file with a JSP, so that when a user requests the JSP, the browser plays music. Specifically, the browser is designed to detect the MIME type of the embedded file, then activate a helper application such as QuickTime or RealAudio to handle the embedded file and play the music.

Example 17-11 shows a JSP that embeds an MPEG, audio layer 3 (MP3) file. The JSP displays some information about the artist based on a request parameter; this random information is included to show how to combine JSP code with the embed tag. The embed tag includes width and height attributes to show the audio controls in the web page. The controls allow the user to turn the volume off or down if they do not want to be serenaded while surfing.

Example 17-11. A JSP with an embedded audio file
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<c:set var="artist" value="${param.artist}" />

<html>
<head><title>Choose Your Tunes</title></head>
<body>

<h2>You chose music from the artist <c:out value="${artist}" /></h2>

<embed src="ConstantCraving.mp3" width="240" height="160">
</embed>

</body>
</html>

Figure 17-6 shows the output from the JSP in Example 17-11.

Figure 17-6. Embedded song file controls in a JSP
figs/jsjc_1706.gif

See Also

Recipe 17.1 and Recipe 17.2 on embedding a Java applet in a JSP; Recipe 17.3-Recipe 17.5 on embedding a Flash file in a JSP; Recipe 17.6 on embedding a QuickTime movie; Recipe 17.7 on embedding an SVG file.

    [ Team LiB ] Previous Section Next Section