Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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 2862 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:

1  
2  
3  
4  
5  
6  
7  
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);
clip.open(ain);
clip.start();
// 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

Thanks,

Jesse
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 803
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....

1  
2  
AudioClip a=this.getAudioClip();
a.play();


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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!