1

I have been trying to get a simple "pew" noise for when my space ship shoots a bullet. The sound is a .wav file that I have saved in the main folder for my netbeans project.

I don't get any errors when I run the code, it just won't play the sound.

In Main.java when the mouse is pressed it shoots a bullet and should play the sound. Sound.java is found below.

Main.java

@Override
public void mousePressed(MouseEvent me) {
    Color color = Color.yellow;
    if(tankMissile == null){
        tankMissile = new Missile(launcher.getXofMissileShoot(), launcher.getYofMissileShoot(), color);
        tankMissile.setTarget(launcher.x, 0);
        int size = 2;
        tankMissile.setExplosionMaxSize(size);
        synchronized (gameData.figures) {
            gameData.figures.add(tankMissile);
        }
    }
    new Sound("pew.wav").start();
}

Sound.java

//www.anyexample.com/programming/java/java_play_wav_sound_file.xml
import java.io.*;
import javax.sound.sampled.*;

public class Sound extends Thread{
    private String fileName;

    public Sound(String wavfile){
        fileName = wavfile;
    }

    @Override
    public void run(){
        File soundFile = new File(fileName);
        if(!soundFile.exists()){
            System.err.println("Wave file not found");
        }

        AudioInputStream audioInputStream = null;
        try{
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        }
        catch(UnsupportedAudioFileException e){
            e.printStackTrace();
            return;
        }
        catch(IOException e){
            e.printStackTrace();
            return;
        }

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try{
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        }
        catch(LineUnavailableException e){
            e.printStackTrace();
            return;
        }
        catch(Exception e){
            e.printStackTrace();
            return;
        }

        if(auline.isControlSupported(FloatControl.Type.PAN)){
            FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
        }

        auline.start();
    }
}

I got it to work finally. This is what I did.

Main.java

@Override
public void mousePressed(MouseEvent me) {
    Color color = Color.yellow;
    if(tankMissile == null){
        tankMissile = new Missile(launcher.getXofMissileShoot(), launcher.getYofMissileShoot(), color);
        tankMissile.setTarget(launcher.x, 0);
        int size = 2;
        tankMissile.setExplosionMaxSize(size);
        synchronized (gameData.figures) {
            gameData.figures.add(tankMissile);
        }

        try {
            playSound("pew.wav");
        } catch (MalformedURLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (LineUnavailableException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

A little later on in Main.java

public static void playSound(String fileName) throws MalformedURLException, LineUnavailableException, UnsupportedAudioFileException, IOException{
    File url = new File(fileName);
    Clip clip = AudioSystem.getClip();

    AudioInputStream ais = AudioSystem.
        getAudioInputStream( url );
    clip.open(ais);
    clip.start();
}
1
  • Take a look at this similar question: stackoverflow.com/questions/2416935/how-to-play-wav-files-with-java
    – John Kane
    Dec 3, 2013 at 15:21

1 Answer 1

1

You can use this method for playing a wav format. You should edit little and mix with your code.

public static synchronized void playSound(final String url) {
  new Thread(new Runnable() {
  // The wrapper thread is unnecessary, unless it blocks on the
  // Clip finishing; see comments.
    public void run() {
      try {
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(
          Main.class.getResourceAsStream("/path/to/sounds/" + url));
        clip.open(inputStream);
        clip.start(); 
      } catch (Exception e) {
        System.err.println(e.getMessage());
      }
    }
  }).start();
}
3
  • Using this I don't get an error still but it isn't playing the sound. Dec 3, 2013 at 15:48
  • It now is saying it is outputting an error of being null so it might just not be finding the sound file. Dec 3, 2013 at 16:04
  • The code I have works and finds the file just fine. It doesn't have any errors and it does get down to the actual playing of the sound it just isn't doing it. Dec 3, 2013 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.