NemoLKLK
NemoLKLK

Reputation: 35

Starting and stopping music files in Java?

This is my sound class.

public class Sounds extends Thread { 

private String filename;
private Position curPosition;
private final int EXTERNAL_BUFFER_SIZE = 524288; 

enum Position { 
    LEFT, RIGHT, NORMAL
};

public Sounds(String wavFile) { 
    filename = wavFile;
    curPosition = Position.NORMAL;
} 

public Sounds(String wavfile, Position p) { 
    filename = wavfile;
    curPosition = p;
} 

public void run() { 

    File soundFile = new File(filename);
    if (!soundFile.exists()) { 
        System.err.println(".wav file not found: " + filename);
        return;
    } 

    AudioInputStream audioInputStream = null;
    try { 
        audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (UnsupportedAudioFileException e1) { 
        e1.printStackTrace();
        return;
    } catch (IOException e1) { 
        e1.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);
        if (curPosition == Position.RIGHT) 
            pan.setValue(1.0f);
        else if (curPosition == Position.LEFT) 
            pan.setValue(-1.0f);
    } 

    auline.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

    try { 
        while (nBytesRead != -1) { 
            nBytesRead = audioInputStream.read(abData, 0, abData.length);
            if (nBytesRead >= 0) 
                auline.write(abData, 0, nBytesRead);
        } 
    } catch (IOException e) { 
        e.printStackTrace();
        return;
    } finally { 
        auline.drain();
        auline.close();
    } 
} 
}

And this is my ActionListener that I want to stop the music when the user clicks a button. The music plays on start up.

btnSound.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        btnSound.setVisible(false);
        btnNoSound.setVisible(true);
        new Sounds("Sounds/MenuMusic.wav").end();
    }
});

I can get the music to play just fine, but I can't figure out how to stop it. I tried using .stop() and .end() but I was told they were deprecated. Anyone have any idea how I can do this? thanks.

Upvotes: 1

Views: 1676

Answers (1)

hyperneutrino
hyperneutrino

Reputation: 5425

You could use the clip class. This is my code for reading a .wav file and playing it:

try {
    File file = new File(src); // src is the location of the file
    AudioInputStream stream = AudioSystem.getAudioInputStream(file);
    Clip clip = AudioSystem.getClip();
    clip.open(stream);
    clip.start();
} catch (Exception e) {
    e.printStackTrace();
}

That was, clip.stop() will stop the clip. To reset the clip, use clip.setFramePosition(0). Note that clip does not need to be closed, because if clip.close() is called after clip.start(), the clip will not play.

Hope that's what you were looking for!

EDIT: Scoping

Using scoping in Java, if you declare a local variable in a method, you cannot access it outside of the method. If you want to access the clip outside of the main method, extract it to a field:

class Main {
    static Clip clip;
    public static void main (String... args) {
        try {
            File file...
            clip = AudioSystem.getClip();
            ...
        } catch...
    }
}

Upvotes: 1

Related Questions