added some audio improvements

This commit is contained in:
shebang
2014-12-18 10:35:15 +01:00
parent 031432f11b
commit b0e30848fa
8 changed files with 68 additions and 37 deletions
@@ -0,0 +1,27 @@
package de.heatup.audio;
public class AudioFiles {
//base path
private static final String basePath = "src/de/heatup/resources/audio/";
//sound files
private String Invaders;
private String YouGetNothing;
public AudioFiles() {
//set paths
this.Invaders = basePath + "8bitinvaders.wav";
this.YouGetNothing = basePath + "yougetnothing.wav";
}
public String getInvaders() {
return this.Invaders;
}
public String getYouGetNothing() {
return this.YouGetNothing;
}
}
+32 -2
View File
@@ -12,7 +12,8 @@ public class AudioPlayer {
public AudioPlayer() {} public AudioPlayer() {}
public void play(String file) { /** plays a file once */
public void play(String file) {
try { try {
final Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class)); final Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class));
@@ -37,5 +38,34 @@ public class AudioPlayer {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(System.out); e.printStackTrace(System.out);
} }
} }
/** loops every given file */
public void loop(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();
}
});
File f = new File(file);
if(!f.exists()) {
System.out.println("File not found: " + f.getAbsolutePath());
return;
}
clip.open(AudioSystem.getAudioInputStream(f));
clip.loop(clip.LOOP_CONTINUOUSLY);
} catch(Exception e) {
e.printStackTrace(System.out);
}
}
} }
+3 -4
View File
@@ -4,11 +4,10 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
SoundfileBomb b = new SoundfileBomb(); AudioFiles f = new AudioFiles();
AudioPlayer p = new AudioPlayer(); AudioPlayer p = new AudioPlayer();
p.play(b.getPath()); //p.loop(f.getInvaders());
p.play(f.getYouGetNothing());
} }
} }
@@ -1,14 +0,0 @@
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;
}
}
@@ -1,14 +0,0 @@
package de.heatup.audio;
public class SoundfileInvaders {
private String path;
public SoundfileInvaders() {
this.path = "src/de/heatup/resources/audio/" + "8bitinvaders.wav";
}
public String getPath() {
return this.path;
}
}
Binary file not shown.
+6 -3
View File
@@ -13,8 +13,8 @@ import java.awt.Point;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import de.heatup.audio.AudioFiles;
import de.heatup.audio.AudioPlayer; import de.heatup.audio.AudioPlayer;
import de.heatup.audio.SoundfileInvaders;
import de.heatup.cfg.Config; import de.heatup.cfg.Config;
import de.heatup.logging.Logger; import de.heatup.logging.Logger;
import de.heatup.mapengine.CollisionHandler; import de.heatup.mapengine.CollisionHandler;
@@ -129,9 +129,12 @@ public class Main extends JFrame implements Runnable {
actionPanel.setVisible(true); actionPanel.setVisible(true);
statusPanel.setVisible(true); statusPanel.setVisible(true);
SoundfileInvaders bgsound = new SoundfileInvaders(); /**
* muss noch in den ClasLoader, loop für background sound geht
*/
AudioFiles f = new AudioFiles();
AudioPlayer p = new AudioPlayer(); AudioPlayer p = new AudioPlayer();
p.play(bgsound.getPath()); p.loop(f.getInvaders());
} }