1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
public class Sound {
private int suspendTime;
private Clip clip;
private String fileURL;
private File soundFile;
private AudioInputStream sound;
private DataLine.Info info;
public Sound(String url){
try {
this.fileURL= url;
soundFile = new File(this.fileURL);
sound = AudioSystem.getAudioInputStream(soundFile);
// chargement du clip
info = new DataLine.Info(Clip.class, sound.getFormat());
this.clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
}
catch(Throwable t){}
}
public void play(){
clip.start();
}
public void loop(){
clip.loop(-1);
}
public void stop(){
suspendTime=0;
clip.setFramePosition(suspendTime);
clip.stop();
}
public void suspend(){
suspendTime = clip.getFramePosition();
clip.setFramePosition(suspendTime);
}
public static void main(String args[]){
Sound s = new Sound("local/gunshot1.wav");
s.play(); // le son est lue
s.play(); // rien ne se passe
s.play(); // rien ne se passe
}
} |
Partager