EOS 2  1.1.0
Einfache Objektbasierte Sprache
Sound.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.base;
2 
3 import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;
4 import static javax.sound.sampled.AudioSystem.getAudioInputStream;
5 
6 import java.io.BufferedInputStream;
7 
8 import javax.sound.sampled.AudioFormat;
9 import javax.sound.sampled.AudioInputStream;
10 import javax.sound.sampled.AudioSystem;
11 import javax.sound.sampled.Clip;
12 import javax.sound.sampled.DataLine;
20 public class Sound {
21  private Clip clip;
26  public Sound(String source) {
27  try {
28  final AudioInputStream stream = getAudioInputStream(new BufferedInputStream(ResourceLoader.getResourceAsStream(source)));
29  final AudioFormat format = getOutFormat(stream.getFormat());
30  DataLine.Info info = new DataLine.Info(Clip.class, format);
31  clip = (Clip) AudioSystem.getLine(info);
32  clip.open(stream);
33  } catch (Exception e) {
34  clip = null;
35  }
36  }
40  public void playLoop() {
41  if (clip != null) {
42  clip.setFramePosition(0);
43  clip.loop(Clip.LOOP_CONTINUOUSLY);
44  clip.start();
45  }
46  }
50  public void play() {
51  if (clip != null) {
52  clip.setFramePosition(0);
53  clip.loop(0);
54  clip.start();
55  }
56  }
60  public void stop() {
61  if (clip != null) {
62  clip.stop();
63  }
64  }
70  private AudioFormat getOutFormat(AudioFormat inFormat) {
71  final int ch = inFormat.getChannels();
72  final float rate = inFormat.getSampleRate();
73  return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
74  }
75 
76 }
static InputStream getResourceAsStream(String filename)
Sound(String source)
Definition: Sound.java:26
Impressum