From 365e7ec79280d9e946329bfe83696d244fa75ad1 Mon Sep 17 00:00:00 2001 From: timbo Date: Tue, 9 Dec 2014 01:35:31 +0100 Subject: [PATCH 1/3] new AudioPlayer tested on windows --- HeatUp/src/de/heatup/audio/AudioPlayer.java | 39 +++++++++++++++++++ HeatUp/src/de/heatup/audio/Main.java | 8 +++- HeatUp/src/de/heatup/audio/Sound.java | 15 +++---- HeatUp/src/de/heatup/audio/SoundfileBomb.java | 14 +++++++ 4 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 HeatUp/src/de/heatup/audio/AudioPlayer.java create mode 100644 HeatUp/src/de/heatup/audio/SoundfileBomb.java diff --git a/HeatUp/src/de/heatup/audio/AudioPlayer.java b/HeatUp/src/de/heatup/audio/AudioPlayer.java new file mode 100644 index 0000000..7ac04bc --- /dev/null +++ b/HeatUp/src/de/heatup/audio/AudioPlayer.java @@ -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); + } + + } + +} diff --git a/HeatUp/src/de/heatup/audio/Main.java b/HeatUp/src/de/heatup/audio/Main.java index 2282518..250dcef 100644 --- a/HeatUp/src/de/heatup/audio/Main.java +++ b/HeatUp/src/de/heatup/audio/Main.java @@ -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()); } } diff --git a/HeatUp/src/de/heatup/audio/Sound.java b/HeatUp/src/de/heatup/audio/Sound.java index 4beda07..4a3ef83 100644 --- a/HeatUp/src/de/heatup/audio/Sound.java +++ b/HeatUp/src/de/heatup/audio/Sound.java @@ -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(); } - } + } } \ No newline at end of file diff --git a/HeatUp/src/de/heatup/audio/SoundfileBomb.java b/HeatUp/src/de/heatup/audio/SoundfileBomb.java new file mode 100644 index 0000000..982443e --- /dev/null +++ b/HeatUp/src/de/heatup/audio/SoundfileBomb.java @@ -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; + } + +} From f91d7f5a2ff19ae824aa880717ffdac6c04a4979 Mon Sep 17 00:00:00 2001 From: timbo Date: Tue, 9 Dec 2014 01:40:09 +0100 Subject: [PATCH 2/3] added comment --- HeatUp/src/de/heatup/audio/AudioPlayer.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/HeatUp/src/de/heatup/audio/AudioPlayer.java b/HeatUp/src/de/heatup/audio/AudioPlayer.java index 7ac04bc..3570834 100644 --- a/HeatUp/src/de/heatup/audio/AudioPlayer.java +++ b/HeatUp/src/de/heatup/audio/AudioPlayer.java @@ -10,8 +10,11 @@ import javax.sound.sampled.LineListener; public class AudioPlayer { - public AudioPlayer(/** String file */) { - ///this.file = file; + public AudioPlayer() { + /** oder anstelle pfad an die methode play, + * immer an die klasse und dann einen Player für jedes Soundfile + */ + } public void play(String file) { @@ -27,7 +30,13 @@ public class AudioPlayer { } }); - clip.open(AudioSystem.getAudioInputStream(new File(file))); + File f = new File(file); + if(!f.exists()) { + System.out.println("File not found: " + f.getAbsolutePath()); + return; + } + + clip.open(AudioSystem.getAudioInputStream(f)); clip.start(); } catch (Exception e) { From a801e601d101fa9b8b6b2e72bd0fef17ec125994 Mon Sep 17 00:00:00 2001 From: shebang Date: Tue, 9 Dec 2014 15:18:42 +0100 Subject: [PATCH 3/3] final audio, multiple sounds are possible and background loop should also work --- HeatUp/src/de/heatup/audio/AudioPlayer.java | 13 ++--- HeatUp/src/de/heatup/audio/Main.java | 5 +- HeatUp/src/de/heatup/audio/Sound.java | 53 --------------------- 3 files changed, 5 insertions(+), 66 deletions(-) delete mode 100644 HeatUp/src/de/heatup/audio/Sound.java diff --git a/HeatUp/src/de/heatup/audio/AudioPlayer.java b/HeatUp/src/de/heatup/audio/AudioPlayer.java index 3570834..98c14a8 100644 --- a/HeatUp/src/de/heatup/audio/AudioPlayer.java +++ b/HeatUp/src/de/heatup/audio/AudioPlayer.java @@ -10,12 +10,7 @@ import javax.sound.sampled.LineListener; public class AudioPlayer { - public AudioPlayer() { - /** oder anstelle pfad an die methode play, - * immer an die klasse und dann einen Player für jedes Soundfile - */ - - } + public AudioPlayer() {} public void play(String file) { @@ -41,8 +36,6 @@ public class AudioPlayer { } catch (Exception e) { e.printStackTrace(System.out); - } - - } - + } + } } diff --git a/HeatUp/src/de/heatup/audio/Main.java b/HeatUp/src/de/heatup/audio/Main.java index 250dcef..f69a460 100644 --- a/HeatUp/src/de/heatup/audio/Main.java +++ b/HeatUp/src/de/heatup/audio/Main.java @@ -6,10 +6,9 @@ public class Main { SoundfileBomb b = new SoundfileBomb(); - //Sound s = new Sound(b.getPath()); - //s.play(); - AudioPlayer p = new AudioPlayer(); p.play(b.getPath()); + + } } diff --git a/HeatUp/src/de/heatup/audio/Sound.java b/HeatUp/src/de/heatup/audio/Sound.java deleted file mode 100644 index 4a3ef83..0000000 --- a/HeatUp/src/de/heatup/audio/Sound.java +++ /dev/null @@ -1,53 +0,0 @@ -package de.heatup.audio; - -import java.io.File; -import java.io.IOException; - -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.Clip; -import javax.sound.sampled.Line; -import javax.sound.sampled.LineUnavailableException; -import javax.sound.sampled.UnsupportedAudioFileException; - -public class Sound { - - private Clip clip; - - Sound(String soundFileName) { - - File f = new File(soundFileName); - if(!f.exists()) { - System.err.println("File not found : " + f.getAbsolutePath()); - return; - } - - try { - - Line.Info linfo = new Line.Info(Clip.class); - Line line = AudioSystem.getLine(linfo); - - clip = (Clip) line; - AudioInputStream inputStream = AudioSystem.getAudioInputStream(f); - - clip.open(inputStream); - - } catch(UnsupportedAudioFileException e) { - e.printStackTrace(); - } catch(IOException e) { - e.printStackTrace(); - } catch(LineUnavailableException e) { - e.printStackTrace(); - } catch(Exception e) { - e.printStackTrace(); - } - } - - public void play() { - try { - clip.start(); - } catch (Exception e) { - e.printStackTrace(); - } - } -} \ No newline at end of file