2 Replies - 390 Views - Last Post: 01 August 2013 - 03:46 AM Rate Topic: -----

#1 Solixious  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 10
  • Joined: 26-July 13

[problem] AudioInputStream

Posted 01 August 2013 - 02:41 AM

I wanted to make a class in java that would produce a sound of a bullet shooting whenever user pressed the respective button. My current code is
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;


public class Sounds
{
	public static void bullet()
	{
		Clip clip=null;
		AudioInputStream ais=null;
		try
		{
			ais=AudioSystem.getAudioInputStream(Sounds.class.getResourceAsStream("bullet.wav"));
			clip=AudioSystem.getClip();
			clip.open(ais);
			clip.start();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		catch(UnsupportedAudioFileException e)
		{
			e.printStackTrace();
		}
		catch(LineUnavailableException e)
		{
			e.printStackTrace();
		}
	}
}



So I use the following code to call this function and produce the sound
Sounds.bullet()



Now the problem that arises is it works properly for sometime, but produces an OutOfMemoryError after that. Then the sound wouldn't play. Can someone provide me an alternative of doing the same job?

Is This A Good Question/Topic? 0
  • +

Replies To: [problem] AudioInputStream

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: [problem] AudioInputStream

Posted 01 August 2013 - 03:34 AM

http://docs.oracle.c...am.html#close()

Do it! (in a finally block)
Was This Post Helpful? 1
  • +
  • -

#3 Solixious  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 10
  • Joined: 26-July 13

Re: [problem] AudioInputStream

Posted 01 August 2013 - 03:46 AM

View Postg00se, on 01 August 2013 - 03:34 AM, said:

http://docs.oracle.c...am.html#close()

Do it! (in a finally block)


Thanks.. My bad... :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1