Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getDuration on mp3 file streams: java.io.IOException: mark/reset not supported

I use getDuration on a local MP3file successfully, but when getDuration on a remote MP3stream results in an 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  at
 sun.net.www.http.KeepAliveStream.reset(KeepAliveStream.java:122)   at
 java.io.FilterInputStream.reset(FilterInputStream.java:226)    at
 sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.reset(HttpURLConnection.java:3299)
    at
 org.tritonus.share.sampled.file.TAudioFileReader.getAudioFileFormat(TAudioFileReader.java:184)
    at
 javax.sound.sampled.AudioSystem.getAudioFileFormat(AudioSystem.java:1004)
    at musicmp3.demoGetlink.getDurationOn(demoGetlink.java:99)  at
 musicmp3.demoGetlink.main(demoGetlink.java:118) Java Result: 1
like image 862
cheng Avatar asked May 29 '26 03:05

cheng


1 Answers

As the line

javax.sound.sampled.AudioSystem.getAudioFileFormat(AudioSystem.java:1004)

seems to be whats throwing the error according to the stack trace. From the spec of AudioSystem

"The implementation of this method may require multiple parsers to examine the stream to determine whether they support it. These parsers must be able to mark the stream, read enough data to determine whether they support the stream, and, if not, reset the stream's read pointer to its original position."

but you appear to be using the stream type sun.net.www.http.KeepAliveStream which in the source returns False for markSupported(). Try creating a new BufferedInputStream from the returned input stream of getInputStream e.g.

InputStream is = urlConnection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

And use that.

Edit: typo

Edit2: Whoops just noticed possible duplicate with java.io.IOException: mark/reset not supported

like image 91
csunday95 Avatar answered May 31 '26 15:05

csunday95