Reliably playing a short sound in Java

后端 未结 6 1416
时光说笑
时光说笑 2021-01-06 04:46

I am tyring to write some Java code that basically just plays a short .wav file - with \'short\' I mean a fraction of a second. (The file I use is at /usr/share/sounds/gener

6条回答
  •  感情败类
    2021-01-06 05:11

    How quickly are you rerunning your calls to play the Clip? I was messing around with making a "wind chime" that had six bell .wav files loaded as Clips, and had some concurrency issues with calls to play the sounds failing. I came up with a scheme that actually made a new Clip on a new thread with each trigger rather than trying to rerun existing Clips and that worked, but I think it is inherently inefficient. (If one is going to the trouble of making new threads anyway, then maybe you might as well run streams and avoid the overhead of loading the entire before playing. I have to test this theory.) BTW: I was able to run close to 100 threads at a time, if I recall correctly. The separate thread approach allows the wav files to complete and "overlap" rather than cut each other off. Fun to watch on JProfiler!

    There are commands that stop a sound and move the starting point back to the beginning. Are you doing that? That might allow reuse in situations where the Clip is called before it has finished.

提交回复
热议问题