Sound-Effekte in java

Habe ich versucht zu bekommen, ein einfaches "pew" Geräusche, wenn mein Raumschiff schießt eine Kugel. Der sound ist eine .wav-Datei, die ich gespeichert habe in den main Ordner für mein netbeans-Projekt.

Ich bekomme keine Fehler wenn ich den code ausführen, es wird nicht nur der Ton abgespielt.

In Main.java wenn die Maus gedrückt wird, es schießt eine Kugel und spielen sollte der sound. Sound.java ist Sie unten finden.

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

Ich habe es auf der Arbeit schließlich. Dies ist, was ich getan habe.

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

Wenig später auf 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();
}
  • Werfen Sie einen Blick auf diese ähnliche Frage: stackoverflow.com/questions/2416935/how-to-play-wav-files-with-java
InformationsquelleAutor RaineShadow | 2013-12-03
Schreibe einen Kommentar