• Advertisement
Sign in to follow this  

Little Help with getting sounds to work in Java.

This topic is 1901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I thought i'd give my shot to load sound to use in an application for events, or what ever well, based on this article i found on it i got thisL

import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;

public class Sound
{
// private variables;
private URL soundUrl;
private AudioInputStream audioIn;
private Clip soundClip;
private String audioFile;

// default constructor.
public Sound()
{
audioFile = " ";
soundClip = null;
soundUrl = null;
audioIn = null;
}

public Sound(String AudioFile)
{
this.audioFile = AudioFile;
}

// load audio file
public boolean loadSoundFile(String file)
{
this.audioFile = file;

try {
// Open an audio input stream.
soundUrl = this.getClass().getClassLoader().getResource(audioFile);
audioIn = AudioSystem.getAudioInputStream(soundUrl);
// Get a sound clip resource.
soundClip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
soundClip.open(audioIn);
}
catch (UnsupportedAudioFileException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (LineUnavailableException e)
{
e.printStackTrace();
}

if(soundUrl != null)
{
return true;
}
else
{
return false;
}
}

// play sound
public void play()
{
this.soundClip.start();
}

public void stop()
{
this.soundClip.stop();
}
}


now it doesn't work the compiler points to this:


audioIn = AudioSystem.getAudioInputStream(soundUrl);


so i don't know what to do.....

Share this post


Link to post
Share on other sites
Advertisement

[quote name='Enerjak' timestamp='1351909150' post='4996736']
now it doesn't work the compiler points to this:

...and gives you what error message? huh.png
[/quote]

sorry a NULL pointer.

Share this post


Link to post
Share on other sites

[quote name='Enerjak' timestamp='1351909150' post='4996736']
now it doesn't work the compiler points to this:

...and gives you what error message? huh.png
[/quote]


Exception in thread "main" java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(Unknown Source)
at javax.sound.midi.MidiSystem.getSequence(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at SoundClipTest.<init>(SoundClipTest.java:21)
at SoundClipTest.main(SoundClipTest.java:37)


sorry, here's the exceptions i get, im following the damn tutorial exactly maybe there's something i need to install?

Share this post


Link to post
Share on other sites
Check that soundUrl is not null, try debugging with System.out.println(soundUrl). ClassLoader.getResource() will return null without throwing an exception if the file name given is not valid. If the url is null make sure that the file you are referencing does indeed exist and that it is relative to the root of your source. For example, if audioFile was something like "audio/sound.wav", then the class loader expects the file to be in src/audio/sound.wav.

Share this post


Link to post
Share on other sites
http://www.javaworld.com/javaqa/2003-08/01-qa-0808-property.html?page=1

Check out the above tutorial about the problems with getResource(), specifically page two. There is Class.getResourceAsStream(), and then ClassLoader.getResourceAsStream(), and they work a little differently.

Enjoy,

Share this post


Link to post
Share on other sites

http://www.javaworld...rty.html?page=1

Check out the above tutorial about the problems with getResource(), specifically page two. There is Class.getResourceAsStream(), and then ClassLoader.getResourceAsStream(), and they work a little differently.

Enjoy,


Thank you and I see we share a love of Sam and Max...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement