Playing sounds for my game

I want to play some sounds for my game. In my “src” folder I have a “resource” folder which contains a folder “sound” contining WAV-files. I want to read and play those. Currently my playing mathod looks like this:



	  public void playLoopedSound() {
	      do{//A do-while-loop to keep playing the sound over and over
		      try {
		          musicStream = (AudioInputStream) SoundPlayer.class.getResourceAsStream("/resources/sound/"+path);
		      } catch (Exception e){
		          e.printStackTrace();
		      }

		      musicFormat = musicStream.getFormat();

		      DataLine.Info info = new DataLine.Info(SourceDataLine.class, musicFormat);
		      try {
		    	  musicSourceLine = (SourceDataLine) AudioSystem.getLine(info);
		    	  musicSourceLine.open(musicFormat);
		      } catch (LineUnavailableException e) {
		          e.printStackTrace();
		      } catch (Exception e) {
		          e.printStackTrace();
		      }

		      musicSourceLine.start();

		      int nBytesRead = 0;
		      byte[] abData = new byte[BUFFER_SIZE];
		      while (running && nBytesRead!=-1) {
		          try {
		              nBytesRead = musicStream.read(abData, 0, abData.length);
		          } catch (IOException e) {
		              e.printStackTrace();
		          }
		          if (nBytesRead >= 0) {
		              @SuppressWarnings("unused")
		              int nBytesWritten = musicSourceLine.write(abData, 0, nBytesRead);
		          }
		      }

		      musicSourceLine.drain();
		      musicSourceLine.close();
	      }while(running);
	  }

This code gives me the following Exception:


java.lang.ClassCastException: java.io.BufferedInputStream cannot be cast to javax.sound.sampled.AudioInputStream
	at project.sound.SoundPlayer.playLoopedSound(SoundPlayer.java:72)
	at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-1" java.lang.NullPointerException
	at project.sound.SoundPlayer.run(SoundPlayer.java:78)
	at java.lang.Thread.run(Unknown Source)

I would really like to have some help. If anyone has a method that works for this I’d love to see it. If it’s complicated to do this I wouldn’t mind a method that plays WAV-files that lie in a folder next to the JAR-file instead of inside it.

I don’t know if this helps but here’s the method I use to load images from a folder which lies next to the “sound” folder in the “resource” folder in the JAR-file. It works perfectly so maybe it can help you imagine how my folders are set up.


public static BufferedImage loadImage(String path){
		URL myImageURL = FileHandler.class.getResource("/resources/images/" + path);
		try {
			BufferedImage myImage = ImageIO.read(myImageURL);
			return myImage;
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("ERROR@FileHandler.loadImage(String path) - IOException with path: " + path); 
			return null;
		} catch(IllegalArgumentException e){
			e.printStackTrace();
			System.out.println("ERROR@FileHandler.loadImage(String path) - IllegalArgumentException with path: " + path);
			return null;
		} 
	}

Ok, now I’ve got it working. There is one thing that confuses me though. If I play a music track in the background and then play a short sound (like a gunshot) with another thread then the volume of the music goes down to about a third of the original volume even after the short sound has ended. Anyone have an idea about why this happens? I’ll put my code for the “playShortSound” method below. The method that plays background music is the same but in a while loop to keep it repeating.


private void playSound(String filename){
		  System.out.println("Going to play the sound " + filename); 
		  try {
	    	  URL url = SoundPlayer.class.getResource("/resources/sound/"+filename);
	    	  audioStream = AudioSystem.getAudioInputStream(url);
	          //musicStream = new AudioInputStream(SoundPlayer.class.getResourceAsStream("/resources/sound/"+path), format, length);
	      } catch (Exception e){
	          e.printStackTrace();
	         //System.exit(1);
	      }

	      audioFormat = audioStream.getFormat();

	      DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
	      try {
	          sourceLine = (SourceDataLine) AudioSystem.getLine(info);
	          sourceLine.open(audioFormat);
	      } catch (LineUnavailableException e) {
	          e.printStackTrace();
	          //System.exit(1);
	      } catch (Exception e) {
	          e.printStackTrace();
	          //System.exit(1);
	      }

	      sourceLine.start();

	      int nBytesRead = 0;
	      byte[] abData = new byte[BUFFER_SIZE];
	      while (nBytesRead != -1) {
	          try {
	              nBytesRead = audioStream.read(abData, 0, abData.length);
	          } catch (IOException e) {
	              e.printStackTrace();
	          }
	          if (nBytesRead >= 0) {
	              @SuppressWarnings("unused")
	              int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
	          }
	      }

	      sourceLine.drain();
	      sourceLine.close();
	      
	      System.out.println("Done playing the sound " + filename); 
	  }