Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Little Help with getting sounds to work in Java.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Enerjak   Members   -  Reputation: 235

Like
0Likes
Like

Posted 02 November 2012 - 08:19 PM

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

Sponsor:

#2 jbadams   Senior Staff   -  Reputation: 19412

Like
0Likes
Like

Posted 03 November 2012 - 12:22 AM

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

...and gives you what error message? Posted Image

#3 Enerjak   Members   -  Reputation: 235

Like
0Likes
Like

Posted 03 November 2012 - 04:46 AM


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

...and gives you what error message? Posted Image


sorry a NULL pointer.

#4 Enerjak   Members   -  Reputation: 235

Like
0Likes
Like

Posted 03 November 2012 - 08:50 AM


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

...and gives you what error message? Posted Image


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?

#5 n3oplasm   Members   -  Reputation: 132

Like
0Likes
Like

Posted 06 November 2012 - 08:10 AM

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.

#6 Glass_Knife   Moderators   -  Reputation: 5046

Like
2Likes
Like

Posted 06 November 2012 - 02:39 PM

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"
Indie Game Programming

#7 Enerjak   Members   -  Reputation: 235

Like
0Likes
Like

Posted 07 November 2012 - 02:38 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS