Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Little Help with getting sounds to work in Java.

Started by Enerjak Nov 3, 2012 at 2:19 AM 5 replies 5.5k views
Original Post
Enerjak
Enerjak
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.....
jbadams
jbadams

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

...and gives you what error message? huh.png
- Jason Astle-Adams
Enerjak
Enerjak

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

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

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.