2 Replies - 2345 Views - Last Post: 31 December 2013 - 04:53 PM Rate Topic: -----

#1 TLHPoE   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 25-September 13

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.

	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) {
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Can't Play Sound More Than Once?

#2 trixt.er   User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 432
  • Joined: 28-September 08

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:

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

Was This Post Helpful? 1
  • +
  • -

#3 TLHPoE   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 25-September 13

Re: Can't Play Sound More Than Once?

Posted 31 December 2013 - 04:53 PM

View Posttrixt.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:

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. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1