1

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ronald Calazans
  • 71
  • 1
  • 11
  • May be best solution use the StreamedContent Support? Description of the documentation on page 295. – 0x5a4d Apr 06 '15 at 13:03
  • There is no need to yell "SOLVED" or "RESOLVED" in title. Stack Overflow is not like an old fashioned and undigestable discussion forum. Once the time allows it, just mark the most helpful answer as accepted in order to let the question appear differently ("has an accepted answer", i.e. "solved") in the listing and search ;) – BalusC Apr 07 '15 at 13:20
  • Ok Balusc, living and learning, nothing of 'resolved' :) – Ronald Calazans Apr 07 '15 at 13:29

2 Answers2

0

like this code skeleton:

*.xhtml

<p:media value="#{mediaBean.media}" width="250" height="225" player="quicktime"/>

@Bean

public class MediaBean {
       private StreamedContent media;
       public MediaController() {
             InputStream stream = new FileInputStream("C://filename.mp3");
             media = new DefaultStreamedContent(stream, "audio/mpeg");
       }
       public StreamedContent getMedia() { return media; }
}

In this example i remove other code for simplify:

@ManagedBean(name = "mediaBean")
@RequestScoped
public class MediaBean{
    public StreamedContent getMedia() throws FileNotFoundException{
            return new DefaultStreamedContent(new FileInputStream("PATH_TO_MEDIA_FILE"),"audio/mpeg");
        }
   }
}

Choose the scope you based on your requirement, in my case it was request.

As explanation About

java.net.SocketException: Broken pipe

and not close stream help this and this posts.

Community
  • 1
  • 1
0x5a4d
  • 750
  • 1
  • 7
  • 21
  • This answer is incomplete. There's no explanation whatsoever how exactly the servlet failed and what scope exactly the bean should be placed in. If the bean is in the wrong scope, this answer would still fail. – BalusC Apr 07 '15 at 07:40
0

I managed to solve :) ... I used to address the response of 0x5a4d and Balusc, with the code 0x5a4d was released a scope error, I'm using in my application the 'Conversation Scope' and launched an exception ... the Balusc commented that the answer was incomplete and could be released this mistake, and that's what happened ...

Then I create a separate Bean only to process the request to MP3 with the 'Default Scope', and it worked ... my class was so. .

@Named
public class AudioBean {
private StreamedContent media;
public AudioBean() throws FileNotFoundException {
      InputStream stream = new FileInputStream("C:\\plataforma\\music.mp3");
    media = new DefaultStreamedContent(stream, "audio/mpeg");
  }
public StreamedContent getMedia() { return media; }

}



and *.xhtml

<p:media value="#{audioBean.media}" 
         width="250" 
         height="225" 
         player="quicktime"/>



Thank you guys for the help!

Ronald Calazans
  • 71
  • 1
  • 11