I have an MP3 audio file outside of the application context, in C:/platform/musig.mp3
.
I'm using the below servlet to serve it.
public class AudioServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream stream = null;
BufferedInputStream buf = null;
try {
stream = response.getOutputStream();
File mp3 = new File("C:/platform/music.mp3");
response.setContentType("audio/mpeg");
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setContentLength((int) mp3.length());
FileInputStream input = new FileInputStream(mp3);
buf = new BufferedInputStream(input);
int readBytes = 0;
while ((readBytes = buf.read()) != -1) {
stream.write(readBytes);
}
} finally {
if (stream != null) {
stream.close();
}
if (buf != null) {
buf.close();
}
}
}
}
<servlet>
<servlet-name>audioServlet</servlet-name>
<servlet-class>servlet.AudioServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>audioServlet</servlet-name>
<url-pattern>/audio/*</url-pattern>
</servlet-mapping>
I'm referencing it in <p:media>
as below:
<p:media id="media"
value="/audio"
player="quicktime"
width="200"
height="40">
<f:param name="autoPlay" value="false" />
</p:media>
The problem is that I am unable to run the sound. If I put the audio file within the application context (in /resources
for example), it works. But out of context, it does not work at all.
The below exception appears in the console when the servlet is invoked:
ClientAbortException: java.net.SocketException: Software Caused connection abort: socket write error
Does anyone have any idea what might be happening? Or is there another way to perform MP3 with the "media" component PrimeFaces I do not know?