Play audio file data - Spring MVC
- by Vijay Veeraraghavan
In my web-application, I have various audio clips uploaded by the users in the database stored in the BLOB column. The audio files are low bit rate WAV files. The clips are secured, one can see only those clips he has uploaded. Instead of user downloading the clip and playing it in his player, I need it be steamed online in the web page itself. In the jsp I use the <audio> tag with the source mapping to the controller mappping url.
<td> <audio controls><source src="recfile/${au.id}" type="audio/mpeg" /></audio> </td>
Where, the recfile is the request mapping and the au.id is the audio id. In the controller I process the request like below
@RequestMapping(value = "/recfile/{id}", method = RequestMethod.GET,
produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
public HttpEntity<byte[]> downloadRecipientFile(@PathVariable("id") int id,
ModelMap model, HttpServletResponse response) throws IOException,
ServletException {
LOGGER.debug("[GroupListController downloadRecipientFile]");
VoiceAudioLibrary dGroup = audioClipService.findAudioClip(id);
if (dGroup == null || dGroup.getAudioData() == null
|| dGroup.getAudioData().length <= 0) {
throw new ServletException("No clip found/clip has not data, id="
+ id);
}
HttpHeaders header = new HttpHeaders();
I tried this too
//header.setContentType(new MediaType("audio", "mp3"));
header.setContentType(new MediaType("audio", "vnd.wave");
header.setContentLength(dGroup.getAudioData().length);
return new HttpEntity<byte[]>(dGroup.getAudioData(), header);
}
When the jsp loads, the controller get the request, it serves back the audio data fetched from the database, the jsp too shows the player with the controls. But when I play it nothing happens. Why is it? Am I missing anything in the configuration? Am I doing it right?