Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting minor sound distortion?

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashish Parekh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..

just use soundobj.prefatch() method.... it will solve ur problem..
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, but im afraid im unfamiliar with this method, could you show me a simple example how you use it. Thanks
 
J. Insi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does it the applet code work while we try in jframe playing wav sound ?
 
Phil Freihofner
Ranch Hand
Posts: 120
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi -
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic