Little Help with getting sounds to work in Java.

Started by
5 comments, last by Enerjak 11 years, 5 months ago
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.....
Advertisement

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

...and gives you what error message? huh.png

- Jason Astle-Adams


[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.

[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?
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.
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,

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532


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...

This topic is closed to new replies.

Advertisement