public void playSound(String name, float volume) {
try {
System.out.println("Playing sound");
Clip clip = AudioSystem.getClip();
AudioInputStream sound = AudioSystem.getAudioInputStream((InputStream) ResourceLoader.instance.getResource(name));
clip.open(sound);
clip.start();
FloatControl fControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
fControl.setValue(volume);
} catch(Exception exception) {
}
}
Can't Play Sound More Than Once?
Page 1 of 12 Replies - 2345 Views - Last Post: 31 December 2013 - 04:53 PM
#1
Can't Play Sound More Than Once?
Posted 30 December 2013 - 07:00 PM
In my game, I play a sound multiple times. If I use this code to play the sound, it only plays the sound on the first try. When I try to play the sound again, nothing is played.
Replies To: Can't Play Sound More Than Once?
#2
Re: Can't Play Sound More Than Once?
Posted 30 December 2013 - 09:30 PM
Well your not giving us the full story. Why do you think it's not playing more than once? It could be a plethora of things. You just provided the method, and that was all. Is this method being called in a loop of some sorts? I noticed a couple things about the Clip object. See below:
Looking above, the close may very well do the trick. You'll have to play around with the flush and isRunning. I don't have any experience with Clip. In the past I used javax.sound.midi which I found was relatively easy to use. Honestly most, if not all, of the Java API is trivial. Clip appears to be fairly basic. I met the inventor of Java. He's a nice gentlemen and goes by the name of James Gosling. He told me that Java was never intended to be complicated (like C/C++ may seem), and that the language was created with new programmers in mind. Hopefully this response gets you started along your way.
Re-edit 11:38 EST: Just one other thought. Why on earth are you not doing anything in your catch block? This is meant to catch expections. Keep in mind that your try may very well be throwing an exception on the second loop. Might I suggestion the following:
while (clip.isRunning()) {} // may need to be called after the clip starts or is opened
clip.flush(); // flushes the clip audio stream
clip.close(); // closes the clip. should be called after audio is played through
Looking above, the close may very well do the trick. You'll have to play around with the flush and isRunning. I don't have any experience with Clip. In the past I used javax.sound.midi which I found was relatively easy to use. Honestly most, if not all, of the Java API is trivial. Clip appears to be fairly basic. I met the inventor of Java. He's a nice gentlemen and goes by the name of James Gosling. He told me that Java was never intended to be complicated (like C/C++ may seem), and that the language was created with new programmers in mind. Hopefully this response gets you started along your way.
Re-edit 11:38 EST: Just one other thought. Why on earth are you not doing anything in your catch block? This is meant to catch expections. Keep in mind that your try may very well be throwing an exception on the second loop. Might I suggestion the following:
catch(Exception e)
{
e.printStackTrace();
}
This post has been edited by trixt.er: 30 December 2013 - 09:41 PM
#3
Re: Can't Play Sound More Than Once?
Posted 31 December 2013 - 04:53 PM
trixt.er, on 30 December 2013 - 09:30 PM, said:
Well your not giving us the full story. Why do you think it's not playing more than once? It could be a plethora of things. You just provided the method, and that was all. Is this method being called in a loop of some sorts? I noticed a couple things about the Clip object. See below:
Looking above, the close may very well do the trick. You'll have to play around with the flush and isRunning. I don't have any experience with Clip. In the past I used javax.sound.midi which I found was relatively easy to use. Honestly most, if not all, of the Java API is trivial. Clip appears to be fairly basic. I met the inventor of Java. He's a nice gentlemen and goes by the name of James Gosling. He told me that Java was never intended to be complicated (like C/C++ may seem), and that the language was created with new programmers in mind. Hopefully this response gets you started along your way.
Re-edit 11:38 EST: Just one other thought. Why on earth are you not doing anything in your catch block? This is meant to catch expections. Keep in mind that your try may very well be throwing an exception on the second loop. Might I suggestion the following:
while (clip.isRunning()) {} // may need to be called after the clip starts or is opened
clip.flush(); // flushes the clip audio stream
clip.close(); // closes the clip. should be called after audio is played through
Looking above, the close may very well do the trick. You'll have to play around with the flush and isRunning. I don't have any experience with Clip. In the past I used javax.sound.midi which I found was relatively easy to use. Honestly most, if not all, of the Java API is trivial. Clip appears to be fairly basic. I met the inventor of Java. He's a nice gentlemen and goes by the name of James Gosling. He told me that Java was never intended to be complicated (like C/C++ may seem), and that the language was created with new programmers in mind. Hopefully this response gets you started along your way.
Re-edit 11:38 EST: Just one other thought. Why on earth are you not doing anything in your catch block? This is meant to catch expections. Keep in mind that your try may very well be throwing an exception on the second loop. Might I suggestion the following:
catch(Exception e)
{
e.printStackTrace();
}
Ah, thanks for the explanation. It works now.
Page 1 of 1

New Topic/Question
Reply


MultiQuote


|