new AudioPlayer tested on windows

This commit is contained in:
timbo
2014-12-09 01:35:31 +01:00
parent 4108d5047f
commit 365e7ec792
4 changed files with 65 additions and 11 deletions
@@ -0,0 +1,39 @@
package de.heatup.audio;
import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
public class AudioPlayer {
public AudioPlayer(/** String file */) {
///this.file = file;
}
public void play(String file) {
try {
final Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class));
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if(event.getType() == LineEvent.Type.STOP)
clip.close();
}
});
clip.open(AudioSystem.getAudioInputStream(new File(file)));
clip.start();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
+7 -1
View File
@@ -4,6 +4,12 @@ public class Main {
public static void main(String[] args) {
Sound.BOMB.play();
SoundfileBomb b = new SoundfileBomb();
//Sound s = new Sound(b.getPath());
//s.play();
AudioPlayer p = new AudioPlayer();
p.play(b.getPath());
}
}
+5 -10
View File
@@ -10,8 +10,7 @@ import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public enum Sound {
BOMB("src/de/heatup/resources/audio/" + "bomb.wav");
public class Sound {
private Clip clip;
@@ -45,14 +44,10 @@ public enum Sound {
}
public void play() {
try {
new Thread() {
public void run() {
clip.start();
}
}.start();
} catch(Throwable e) {
try {
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@@ -0,0 +1,14 @@
package de.heatup.audio;
public class SoundfileBomb {
private String path;
public SoundfileBomb() {
this.path = "src/de/heatup/resources/audio/" + "bomb.wav";
}
public String getPath() {
return this.path;
}
}