Home > database >  How do I make a clip outside of method?
How do I make a clip outside of method?

Time:05-30

I created a method that either starts or stops music when called, but if I call it more than once, it starts a duplicate clip. I can't find out how to make the audio clip variable outside of the variable to prevent this.

    public static void playSound(boolean play) throws Exception {
    File sound = new File("wave.wav");
    Clip clip = AudioSystem.getClip();
    if(play=true) {
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(sound);
        inputStream = AudioSystem.getAudioInputStream(sound);
        clip.open(inputStream);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        clip.start();
    } else if (play=false) {
        clip.stop();
    }
}

UPDATE: So after a few hours of trying, I decided to just put the code to start music in the main method, make the clip variable static (so eclipse would let me use it in event) and then used clip.stop() on the event. Just thought I'd share.

CodePudding user response:

Your immediate issue is if(play=true) {, this is setting play to true and therefore will reopen the clip and start playing it again every time.

You should be using if(play==true) { to determine if the value of play is true, but you could also use if(play) {

Take a look at The if-then and if-then-else Statements for more details.

Essentially, = is for assignment == is for evaluation. An if condition must equate to a boolean (true/false), so when using a boolean value you don't have to use ==, you can just use the value itself.

Assuming play is true, both if (play == true) and if (play) will evaluate to true

Having said that, once you have the clip initialised and open, you don't need to re-open it when you want to start playing again, for example...

public class Track {
    private Clip clip;
    private File source;

    public Track(File source) {
        this.source = source;
    }

    public static void playSound(boolean play) throws Exception {
        File sound = new File("wave.wav");
        Clip clip = AudioSystem.getClip();
        if (play = true) {
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(sound);
            inputStream = AudioSystem.getAudioInputStream(sound);
            clip.open(inputStream);
            clip.loop(Clip.LOOP_CONTINUOUSLY);
            clip.start();
        } else if (play = false) {
            clip.stop();
        }
    }

    public File getSource() {
        return source;
    }

    public Clip getClip() {
        return clip;
    }

    public void play() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
        if (clip == null) {
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(getSource());
            inputStream = AudioSystem.getAudioInputStream(getSource());
        }
        if (!clip.isOpen()) {
            clip.open();
        }
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        clip.start();
    }
    
    public void stop() {
        if (clip == null) {
            return;
        }
        if (!clip.isActive()) {
            return;
        }
        clip.stop();
        clip.setFramePosition(0);
    }
}

In fact you could also easily add support for pausing by doing something like

public void pause() {
    if (clip == null) {
        return;
    }
    if (!clip.isActive()) {
        return;
    }
    clip.stop();
}

(ps - I didn't add a close method which would also be pretty useful)

CodePudding user response:

In addition to the more complete answer, having the statement read like

if(play)
{
    // Play code
}
else
{
    clip.stop();
}

Instead of

if(play == true)
{
    // Play code
}
else if(play == false)
{
    clip.stop();
}

Is much better style

  •  Tags:  
  • java
  • Related