Coupon22
Coupon22

Reputation: 394

Using a URL with AudioInputStream

            URL url = new URL("http://www.dasolorfire.freehostia.com/sound/test.wav");
            AudioInputStream sound = AudioSystem.getAudioInputStream(url); //Sound.java:50
            DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
            clip = (Clip) AudioSystem.getLine(info);
            clip.open(sound);
            clip.addLineListener(new LineListener()
            {
                public void update(LineEvent event)
                {
                    if (event.getType() == LineEvent.Type.STOP)
                    {
                        event.getLine().close();
                    }
                }
            });
            clip.start();

This is my attempt at loading audio from a url. Not only is this very slow, but when I try to load the sound file I get this exception:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at srcD.Sound.soundPlay(Sound.java:50)

My next attempt is replacing the url with an input stream.

            InputStream url = new URL("http://www.dasolorfire.freehostia.com/sound/test.wav").openStream();
            AudioInputStream sound = AudioSystem.getAudioInputStream(url);

The problem with this is that I get this error:

java.io.IOException: mark/reset not supported
at java.io.InputStream.reset(Unknown Source)
at java.io.FilterInputStream.reset(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.reset(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at srcD.Sound.soundPlay(Sound.java:50)

(I am trying to load sound for a jnlp)

Upvotes: 2

Views: 6578

Answers (2)

Bjorn Roche
Bjorn Roche

Reputation: 11469

As far as the second error goes, you may be able to get this to work by wrapping the input stream in a BufferedInputStream like this:

        InputStream is = new URL("http://www.dasolorfire.freehostia.com/sound/test.wav").openStream();
        BufferedInputStream bis = new BufferedInputStream( is );
        AudioInputStream sound = AudioSystem.getAudioInputStream(bis);

Upvotes: 3

jarnbjo
jarnbjo

Reputation: 34313

The first error (could not get audio input stream from input URL) indicates that the format of the audio data you are referring to is not supported. I don't know if your example code uses a real URL, but if I try to invoke http://www.dasolorfire.freehostia.com/sound/test.wav, I am redirected to http://www.freehostia.com/ and get an HTML page, not the expected WAV file.

The second error (mark/reset not supported) should be self explanatory. The API documentation for AudioSystem.getAudioInputStream(InputStream) explains why the provided InputStream must support the mark and reset features (these features are optional for specific InputStream implementations).

Upvotes: 1

Related Questions