getDuration on mp3 file streams: java.io.IOException: mark/reset not supported -
i use getduration on local mp3file successfully, when getduration on remote mp3stream results in error: java.io.ioexception: mark/reset not supported.
successful getduration on local mp3:
public static void getdurationoff() throws unsupportedaudiofileexception, ioexception { int sumtime = 0; file file = new file("d:\\java\\musicmp3\\src\\images\\water_lily.mp3"); audiofileformat fileformat = audiosystem.getaudiofileformat(file); if (fileformat instanceof taudiofileformat) { map<?, ?> properties = ((taudiofileformat) fileformat).properties(); long microseconds = (long) properties.get("duration"); //total seconds sumtime = (int)(microseconds / 1000000); system.out.println("total seconds :"+sumtime); } }
failed getduration on remote mp3:
public static void getdurationon() throws unsupportedaudiofileexception, ioexception { int sumtime = 0; string linkonline="http://api.mp3.zing.vn/api/mobile/source/song/lmjnykgnlnmnnkutzvctbgzm"; urlconnection urlconnection = new url(linkonline).openconnection(); urlconnection.connect(); audiofileformat fileformat = audiosystem.getaudiofileformat(urlconnection.getinputstream()); if (fileformat instanceof taudiofileformat) { map<?, ?> properties = ((taudiofileformat) fileformat).properties(); long microseconds = (long) properties.get("duration"); //total seconds sumtime = (int)(microseconds / 1000000); system.out.println("total seconds :"+sumtime); } }
error:
exception in thread "main" java.io.ioexception: mark/reset not supported @ sun.net.www.http.keepalivestream.reset(keepalivestream.java:122) @ java.io.filterinputstream.reset(filterinputstream.java:226) @ sun.net.www.protocol.http.httpurlconnection$httpinputstream.reset(httpurlconnection.java:3299) @ org.tritonus.share.sampled.file.taudiofilereader.getaudiofileformat(taudiofilereader.java:184) @ javax.sound.sampled.audiosystem.getaudiofileformat(audiosystem.java:1004) @ musicmp3.demogetlink.getdurationon(demogetlink.java:99) @ musicmp3.demogetlink.main(demogetlink.java:118) java result: 1
as line
javax.sound.sampled.audiosystem.getaudiofileformat(audiosystem.java:1004)
seems whats throwing error according stack trace. spec of audiosystem
"the implementation of method may require multiple parsers examine stream determine whether support it. these parsers must able mark stream, read enough data determine whether support stream, and, if not, reset stream's read pointer original position."
but appear using stream type sun.net.www.http.keepalivestream in source returns false marksupported(). try creating new bufferedinputstream returned input stream of getinputstream e.g.
inputstream = urlconnection.getinputstream(); bufferedinputstream bis = new bufferedinputstream(is);
and use that.
edit: typo
edit2: whoops noticed possible duplicate java.io.ioexception: mark/reset not supported
Comments
Post a Comment