-1

I have here a code which listens to the input of the user.

System.out.println(message);{
            if(message.equals("play")){
                try {
                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:/DancingMachupapet/Test.wav"));
                        Clip clip = AudioSystem.getClip();
                        // getAudioInputStream() also accepts a File or InputStream

                        clip.open(inputStream);
                        clip.start();
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                JOptionPane.showMessageDialog(null, "Close to exit!");
                            }
                        });


                }catch (IOException e) {
                }
            }
            if(message.equals("hoy")){
                try {
                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:/DancingMachupapet/ThinkOfManu.wav"));
                        Clip clip = AudioSystem.getClip();
                        // getAudioInputStream() also accepts a File or InputStream

                        clip.open(inputStream);
                        clip.start();
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                JOptionPane.showMessageDialog(null, "Close to exit!");
                            }
                        });


                }catch (IOException e) {
                }
            }
            }

THis code works when it sees the message 'hoy' and 'play' it will play 'ThinkOfManu.wav' and 'Test.wav' repectively. It works but my problem is when i simultaneously input the 'hoy' and 'play', the 2 wav files plays as well simultaneously.

What I want to happen is that when ThinkOfManu.wav is playing and I call Test.wav, the ThinkOfManu should stop and play the Test.wav. NOT SIMULTANEOUSLY.

Please help me here.

Kerv
  • 179
  • 1
  • 5
  • 15
  • Possible duplicate of [Play audio clips sequentially in JApplet](http://stackoverflow.com/questions/16614943/play-audio-clips-sequentially-in-japplet). Since you apparently ignored my advice in [your last question](http://stackoverflow.com/q/18685193/418556), that also comes with a -1. – Andrew Thompson Sep 08 '13 at 17:02

1 Answers1

1

I am assuming that a method Clip.stop does exist?

You are creating a new clip object each time you are running a new song. Don't do that, instead initialize the clip object outside your try-catch block, and just before triggering clip.play(), do a clip.stop.

That should work for you.

Example code:

Clip clip = AudioSystem.getClip();

if(message.equals("play"))
{
  try {
       AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:/DancingMachupapet/Test.wav"));
    clip.stop();
    clip.open(inputStream);
    clip.start();
    SwingUtilities.invokeLater(new Runnable() {
       public void run() {
       JOptionPane.showMessageDialog(null, "Close to exit!");
    }});
  }catch (IOException e) {
  }
  }
            if(message.equals("hoy")){
                try {
                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:/DancingMachupapet/ThinkOfManu.wav"));
                        clip.open(inputStream);
                        clip.start();
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                JOptionPane.showMessageDialog(null, "Close to exit!");
                            }
                        });


                }catch (IOException e) {
                }
            }
            }
Neeraj
  • 8,408
  • 8
  • 41
  • 69
  • WHere will I place clip.stop();? I removed my try-catch block and still it plays simultaneously @Neeraj – Kerv Sep 08 '13 at 16:54
  • Also, if I were you, I wont use this approach. Use a Hashmap of "command" : "filepath", and create a function which takes the command, and plays the related file, taking the value from a map. – Neeraj Sep 08 '13 at 18:38