0

I have been trying to play music in my app. I've been using the example BigClip code:

try {
        url = new URL(Sounds.class.getResourceAsStream("title1.wav").toString());
        } catch (MalformedURLException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        BigClip clip = new BigClip();
        AudioInputStream ais = null;
        try {
            ais = AudioSystem.getAudioInputStream(url);
        } catch (UnsupportedAudioFileException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            clip.open(ais);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        clip.start();
        JOptionPane.showMessageDialog(null, "BigClip.start()");
        clip.loop(4);
        JOptionPane.showMessageDialog(null, "BigClip.loop(4)");
        clip.setFastForward(true);
        clip.loop(8);
        // the looping/FF combo. reveals a bug..
        // there is a slight 'click' in the sound that should not be audible
        JOptionPane.showMessageDialog(null, "Are you on speed?");
}

When I only use title1.wav, I get this error:

java.net.MalformedURLException: no protocol: java.io.BufferedInputStream

When I add the protocol file://, I get a NullPointerException, although I can't see what could be causing that.

Am I using the wrong protocol, or have I done something else wrong? Thanks in advance!

Johny
  • 2,128
  • 3
  • 20
  • 33
Noodly_Doodly
  • 69
  • 2
  • 11

1 Answers1

0

Assuming your file is in the same package ("directory") as the Sounds class, use

url = Sounds.class.getResource("title1.wav");

because

new URL(Sounds.class.getResourceAsStream("title1.wav").toString())

is just bound not to work. You are calling toString on an instance of InputStream.

The NPE probably happens because AudioSystem.getAudioInputStream fails due to bad URL path so ais is null and BigClip throws NPE from open.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • Thank you, it works well now! Is there a reason why BigClip will cause the audio to start, stop, play the start again, keep going, and glitching? – Noodly_Doodly Nov 13 '14 at 12:45
  • I'm not very familiar with BigClip. If you mean the one by @AndrewThompson [here](http://stackoverflow.com/a/9470886/2891664) you might ask a new question about it and get his attention. (Post a comment to one of his answers with a link or ping him like I just did.) Though could be it is answerable by someone else. – Radiodef Nov 13 '14 at 19:36