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!
Assuming your file is in the same package ("directory") as the Sounds class, use
because
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 soais
is null andBigClip
throws NPE fromopen
.