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  
  Playing the same sound many times  (Read 4036 times)
0 Members and 1 Guest are viewing this topic.
Offline Max Pesce

Junior Member




Nil tam arduum quod non ingenio vincas


« Posted 2003-07-26 04:54:05 »

My problem is that I don't know how to play play the same clip many times simultaneously...

If an event in the game happens two times I must play the same clip two times.

This is the code I use now:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
if(!soundOn) return true;
           
            Clip snd=getSound(name);
           
            if(snd==null){
                  Debug.err("! SoundBank.playSound() Sound '"+name+"' not present!");
                  return false;
            }
           
            if(snd.isRunning()){
                  snd.stop();
            }
            snd.setMicrosecondPosition(0);
            snd.start();


Infact I should not stop the sound, but run another
"instance"...

Any idea?

Offline aikarele

Senior Newbie





« Reply #1 - Posted 2003-07-26 06:40:33 »

I would suggest that you load your clip to a byte array and then use AudioSystem.getLine to get a new line for each clip. Here is a short example how to play audio.wav 32 times simultaneously (i.e. 32 polyphony):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
import javax.sound.sampled.*;
import java.io.*;

public class MultiClip {
   public static void main(String[] args) {
      try {
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("audio.wav"));
         AudioFormat af                    = audioInputStream.getFormat();
         int size                          = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
         byte[] audio                      = new byte[size];
         DataLine.Info info                = new DataLine.Info(Clip.class, af, size);
         audioInputStream.read(audio, 0, size);

         for(int i=0; i < 32; i++) {
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(af, audio, 0, size);
            clip.start();
         }
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   }
}
8: Undefined index: online
File: /home/jgo/public_html/Themes/default/Display.template.php (main sub template - eval?)
Line: 151