This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi there, im developing a space shooter and have just recently learned how to include sounds, it works fine except when firing the same soundclip over and over again very fast.
Here is the code I use for the sound:
This code above is within the class that "owns" that sound, it superclass has a method to generate the sound:
For some reason there is less distortion when I reassign sound to JApplet.newAudioClip(url).
now the main class can call that method to play the sound. My thread calls this method every 60ms while I hold down right mouse button generating a machinegun firing sound.
This works great except the game stops responding every once in awhile for about 5ms or something, not that serious but noteworthy.
How can I accomplish this task without that distortion? That is replay the same sound clip every 60ms.
By the way the soundclip is in .wav format, lowest quality.
I have only used the javax.sound.sampled libraries for playing sounds, but maybe some of what happens there also applies to your case.
A sound should probably be played on its own thread, as it can block (prevent other things from executing while it is playing).
Also, there might be problems if you try to play a sound again before it's finished playing.
Last, if you preload sounds (in the javax.sound.sampled libraries, this is done with a Clip object), they can be played quickly. So, if there is a way to preload your sound, maybe pre-make an array of as many as will ever sound at the same time (including overlapping) and launch them from their own threads.
This could be terrible advice, since I haven't become acquainted with the Applet's Sound support.