Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Fast way to play sounds in Java Applet  (Read 3913 times)
0 Members and 1 Guest are viewing this topic.
Offline Jesse_Attard

Senior Newbie

« Posted 2009-04-12 14:25:02 »

Saw a post slightly below this asking same question but didn't find an answer so I'll repost his question since I am having the same problem.

I am playing small 22khz 16 bit WAV sound clips in a Java Applet using the following code:

InputStream is = loadFile(filename);
AudioInputStream ain = AudioSystem.getAudioInputStream(is);
DataLine.Info info = new DataLine.Info(Clip.class, ain.getFormat());      
Clip clip = (Clip)AudioSystem.getLine(info);;
// I close the clip object with a linelistener once it's done playing

Works great, but takes about 6ms to execute, even on sounds that have already been loaded. I need to be able to play this sound more than once, and allow overlapping of that same sound.

Would like something more responsive - anyone have a faster method to play sound?

Some things I have tried:
- Holding a reference to the clip instance rather than reinstantiating seems to drop from 6ms to 2ms, but then repeating the same sound interrupts itself... I could get around this by keeping say, 4 references to the same clip but I'm also having issues with sounds entirely cutting out (never playing again) using this method.
- The basic play(getCodeBase(), filename) stuff is slow and doesn't allow overlapping whatsoever
- OGG code streaming in a separate thread, this seems decent for music but for sound clips not responsive enough


Offline Riven

« JGO Overlord »

Medals: 1340
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #1 - Posted 2009-04-12 15:14:17 »

Load the file in a byte[] and create a new ByteArrayInputStream for every play.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Jesse_Attard

Senior Newbie

« Reply #2 - Posted 2009-06-11 03:26:38 »

Been a while before I was able to try this out, but wanted to post thanks because this totally worked and solved the problem.  Plays mega fast now
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #3 - Posted 2009-06-11 17:15:03 »

looking for a mor simple, faster and more stable way ? humm....

AudioClip a=this.getAudioClip();;

and you can overlapp same sound if you use toolkit.newAudioClip();

Pages: [1]
  ignore  |  Print  

EgonOlsen (73 views)
2018-06-10 19:43:48

EgonOlsen (54 views)
2018-06-10 19:43:44

EgonOlsen (73 views)
2018-06-10 19:43:20

DesertCoockie (253 views)
2018-05-13 18:23:11

nelsongames (154 views)
2018-04-24 18:15:36

nelsongames (154 views)
2018-04-24 18:14:32

ivj94 (895 views)
2018-03-24 14:47:39

ivj94 (156 views)
2018-03-24 14:46:31

ivj94 (808 views)
2018-03-24 14:43:53

Solater (172 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!