2

I want to play mp3 file from server server side code:

if (uri.contains("mp3")) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(audioFile.getAbsoluteFile());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return newFixedLengthResponse(Response.Status.OK, MIME_TYPES.get("mp3"), fis,audioFile.getTotalSpace());
    }

while I am calling

192.168.0.7:XXXX/mp3

Error throw but played mp3 file in browser so why this error occure:

 java.net.SocketException: Broken pipe
                                                                                          at java.net.SocketOutputStream.socketWrite0(Native Method)
                                                                                          at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:112)
                                                                                          at java.net.SocketOutputStream.write(SocketOutputStream.java:157)
                                                                                          at fi.iki.elonen.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1694)
                                                                                          at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1667)
                                                                                          at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1657)
                                                                                          at fi.iki.elonen.NanoHTTPD$Response.send(NanoHTTPD.java:1624)
                                                                                          at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:957)
                                                                                          at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192)
                                                                                          at java.lang.Thread.run(Thread.java:761)
Rajesh Gauswami
  • 572
  • 7
  • 22
  • I have a similar issue. I think that, since the browser doesn't know the size on the first call they only get the first junk of the file a first response. Then they try and resume the the download (content-range) and this exception occurs. Since the first connection is already dropped, but the buffered writer for the INITIAL response still tries to write to the output stream, this exception occurs. Second request (PARTIAL_CONTENT) gets a new response. This is my theory, need to prove (debug) it though! – joe1806772 Apr 16 '20 at 15:12

1 Answers1

1

Broken pipe usually happen when someone (here the server) is trying to write in a socket that was closed on the other side (here the client).

You should probably use length() (size of the file) instead of getTotalSpace() (size of the partition)

You could also try to use a chunked response:

return newChunkedResponse(Response.Status.OK, MIME_TYPES.get("mp3"), fis);
bwt
  • 17,292
  • 1
  • 42
  • 60
  • I tried your suggestion but error still there" return newChunkedResponse(Response.Status.OK, MIME_TYPES.get("mp3"), fis);" – Rajesh Gauswami Dec 07 '16 at 11:34
  • Where does it happen this time, could you post the stacktrace ? – bwt Dec 07 '16 at 11:42
  • same when I am calling 192.168.0.7:XXXX/mp3 – Rajesh Gauswami Dec 07 '16 at 11:59
  • It seems that for some reason the browser closes the stream prematurely. Is the mp3 played fully ? what happen whith other browsers ? with other type of files (does the browser download them completely) ? – bwt Dec 07 '16 at 12:26
  • mp3 playing fine infect playing whole but i don't understand why this error occur. – Rajesh Gauswami Dec 07 '16 at 12:27
  • So the important part of the file is actualy transmitted and the error happen at the end. It may be because the player use a method to know when the streams ends which does not match exactly the file size. What happen if you try to download a binary file ? – bwt Dec 07 '16 at 13:48