KiranGautam
KiranGautam

Reputation: 150

Java sound doesn't work after I stop it

This is just the test file, I have my Sound class and I am invoking it from another main class. It plays for 3 sec, and then it stops, then it doesn't play again. What might be the issue?

This is driving me crazy.

Sound Class

public class Sound {

    private static URL url1, url2, url3, url4;
    private static AudioClip gun, boom;
    private static Clip menuClip, gameClip;
    private static AudioInputStream menu, game;

    public static boolean menuPlaying, gamePlaying, explosionOn;

    public Sound() {
        menuPlaying = false;
        gamePlaying = false;
        url1 = Sound.class.getResource("/laserSound.wav");

        url2 = Sound.class.getResource("/boom.wav");

        url3 = Sound.class.getResource("/adventure.wav");

        url4 = Sound.class.getResource("/adventure.wav");

        gun = Applet.newAudioClip(url1);

        boom = Applet.newAudioClip(url2);

        try {
            game = AudioSystem.getAudioInputStream(url4);
            //menu = AudioSystem.getAudioInputStream(url3);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void playGunSound() {
        gun.play();
    }

    public void stopGunSound() {
        gun.stop();
    }

    public void playExplosion() {
        boom.stop();
        boom.play();
    }

    public void stopExplosion() {
        boom.stop();
    }
    public void playGameMusic() throws LineUnavailableException {
        if (gamePlaying == false) {
            gameClip = AudioSystem.getClip();
            try {
                gameClip.open(game);
            } catch (
            LineUnavailableException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            gameClip.start();
            gameClip.loop(Clip.LOOP_CONTINUOUSLY);
            gamePlaying = true;
        }
    }

    public void stopGameMusic() {
            gameClip.stop();
            gamePlaying = false;

    } 

}

Main Method

public static void main(String[] args) throws LineUnavailableException, InterruptedException {
    Sound sound = new Sound();
    sound.playGameMusic();
    TimeUnit.SECONDS.sleep(3);
    sound.stopGameMusic();
    TimeUnit.SECONDS.sleep(3);
    sound.playGameMusic();
}

Upvotes: 3

Views: 251

Answers (1)

Phil Freihofner
Phil Freihofner

Reputation: 7910

The way the code stands now, there are a couple curious things that should be cleaned up.

First off, it would be good to put the gameClip.open in a separate method. You only have to open a Clip once. As long as you don't close it, you can start and stop it at will. As it stands you are opening the clip with each play, and every time you do this, the entire sound file has to be loaded into memory before the Clip will play. This defeats the purpose of the Clip object.

Secondly, having start followed by loop is redundant. Either will play the Clip. If you want continuously looping playback, just call the loop method, not start.

After you clean up these two things, let's revisit the code and see if the problems have gone away or not. I'm not clear if whether the problem that is occurring is due to a specific error or if one of your two redundant code operations is creating an undefined/unexpected situation.

Have you looked at the Clip API?

I'm not experienced with AudioClip. But I've used Clip successfully.

Upvotes: 1

Related Questions