3

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();
}
nhouser9
  • 6,730
  • 3
  • 21
  • 42
KiranGautam
  • 142
  • 1
  • 11
  • did you try to debug it? what about if you instantiate a new `Sound` object? – Scary Wombat Feb 24 '17 at 04:09
  • Yes it works when i instantiate new Sound object. Still dont know why its not working without instantiating but thank you for your help... :) @ScaryWombat – KiranGautam Feb 24 '17 at 04:42
  • Probably you need to release the resource after you call stop method, so that it plays again on calling playmusic – SoulRayder Feb 24 '17 at 05:06
  • I'd suggest using a Java Sound based `Clip` for this. They put a lot of thought & work into the `Clip` to make it easier for programmers to use & take care of the fine details that can trip us up. – Andrew Thompson Feb 24 '17 at 19:19

1 Answers1

1

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.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • Thank you very much. Putting gameClip.open in a separate method solved it also i removed redundant start, it works perfect. Thank you for your help. – KiranGautam Feb 25 '17 at 09:23