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;
		} 
	}