5 Replies - 501 Views - Last Post: 01 November 2011 - 07:10 PM Rate Topic: -----

#1 taskforce141  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 61
  • Joined: 04-July 11

Importing Web Sound

Posted 01 November 2011 - 06:35 PM

I'm currently in the process of making a Java video game. All of the game play is done, but now I am putting in sound effects. I obtained some code and altered a bit to help me on the subject. It is a class where you input the URL of the sound you are trying to input. Here is the class...

import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
   
/**
 * This enum encapsulates all the sound effects of a game, so as to separate the sound playing
 * codes from the game codes.
 * 1. Define all your sound effect names and the associated wave file.
 * 2. To play a specific sound, simply invoke SoundEffect.SOUND_NAME.play().
 * 3. You might optionally invoke the static method SoundEffect.init() to pre-load all the
 *    sound files, so that the play is not paused while loading the file for the first time.
 * 4. You can use the static variable SoundEffect.volume to mute the sound.
 */
public class SoundEffect {
   
   // Nested class for specifying volume
   public static enum Volume {
      MUTE, LOW, MEDIUM, HIGH
   }
   
   public static Volume volume = Volume.LOW;
   
   // Each sound effect has its own clip, loaded with its own sound file.
   private Clip clip;
   
   // Constructor to construct each element of the enum with its own sound file.
   SoundEffect(String soundFileName) {
      try {
         // Use URL (instead of File) to read from disk and JAR.
         URL url = this.getClass().getClassLoader().getResource(soundFileName);
         // Set up an audio input stream piped from the sound file.
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
         // Get a clip resource.
         clip = AudioSystem.getClip();
         // Open audio clip and load samples from the audio input stream.
         clip.open(audioInputStream);
      } catch (UnsupportedAudioFileException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (LineUnavailableException e) {
         e.printStackTrace();
      }
   }
   
   // Play or Re-play the sound effect from the beginning, by rewinding.
   public void play() {
      if (volume != Volume.MUTE) {
         if (clip.isRunning())
            clip.stop();   // Stop the player if it is still running
         clip.setFramePosition(0); // rewind to the beginning
         clip.start();     // Start playing
      }
   }
}



Now the URL I am using is "http://www.shockwave-sound.com/sound-effects/explosion-sounds/Explosion7.wav"

I then get a run time error...

java.lang.NullPointerException
	at com.sun.media.sound.WaveFileReader.getAudioInputStream(Unknown Source)
	at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
	at SoundEffect.<init>(SoundEffect.java:32)
	at Level1.<init>(Level1.java:74)
	at planeGame.main(planeGame.java:9)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)



I have a feeling I am inputting the URL in an incorrect way. Any assistance on how to use this class with a URL will be greatly appreciated. Thanks in advance.

This is how I am inputting the URL.
SoundEffect explosion1;
explosion1 = new SoundEffect("http://www.shockwave-sound.com/sound-effects/explosion-sounds/Explosion7.wav");



Is This A Good Question/Topic? 0
  • +

Replies To: Importing Web Sound

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Importing Web Sound

Posted 01 November 2011 - 06:41 PM

What's the string you are passing? You know for \ characters you have to escape them like: \\
Was This Post Helpful? 0
  • +
  • -

#3 taskforce141  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 61
  • Joined: 04-July 11

Re: Importing Web Sound

Posted 01 November 2011 - 06:56 PM

The string I am passing is...

"http://www.shockwave-sound.com/sound-effects/explosion-sounds/Explosion7.wav"

I am aware of what you said, but would that apply to a forward slash as well, since this is a URL instead of a file location?
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Importing Web Sound

Posted 01 November 2011 - 06:59 PM

The url is null. The Returns section from ClassLoader.getResource(String):

Quote

A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.


I'm thinking it's the second condition. Try this:

URL url = new URL( soundFileName );


I wasn't able to hear the entire sample, I heard a chirp. Lol.

This post has been edited by blackcompe: 01 November 2011 - 07:07 PM

Was This Post Helpful? 1
  • +
  • -

#5 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Importing Web Sound

Posted 01 November 2011 - 07:05 PM

You will have to surround that instantiation in a try catch statement:

see http://download.orac...eatingUrls.html
Was This Post Helpful? 0
  • +
  • -

#6 taskforce141  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 61
  • Joined: 04-July 11

Re: Importing Web Sound

Posted 01 November 2011 - 07:10 PM

View Postblackcompe, on 01 November 2011 - 06:59 PM, said:

The url is null. The Returns section from ClassLoader.getResource(String):

Quote

A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.


I'm thinking it's the second condition. Try this:

URL url = new URL( soundFileName );


I wasn't able to hear the entire sample, I heard a chirp. Lol.

Thanks a bunch. Works Great!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1