diff --git a/src/de/jaujau/UnitTests/TEST_Audio.java b/src/de/jaujau/UnitTests/TEST_Audio.java new file mode 100755 index 0000000..4faca77 --- /dev/null +++ b/src/de/jaujau/UnitTests/TEST_Audio.java @@ -0,0 +1,24 @@ +package de.jaujau.UnitTests; + + + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import de.jaujau.daten.Audio; + +class TEST_Audio { + + Audio sound; + + @BeforeEach + void setUp() throws Exception { + sound = new Audio(); + } + + @Test + void test() { + sound.spieleJauJau(); + } + +} diff --git a/src/de/jaujau/daten/Audio.java b/src/de/jaujau/daten/Audio.java new file mode 100755 index 0000000..fb0330d --- /dev/null +++ b/src/de/jaujau/daten/Audio.java @@ -0,0 +1,76 @@ +package de.jaujau.daten; + + +import java.io.IOException; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.SourceDataLine; + +/** + * + * @author Sebastian Kacza + * + */ +public class Audio { + + public Audio() { + + } + + + /** + * Spielt den JauJau Ton ab + */ + public void spieleJauJau() { + try { + playSound("/sound/jaujau.wav"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + /* + * !!! DIESE KLASSE IST NICHT VON MIR !!! + * Quelle: https://stackoverflow.com/questions/2416935/how-to-play-wav-files-with-java + */ + /** + * Spiel eine Audio dateim im WAV-Format die im internen Ordner gespeichert ist ab. + * @param path Ptad zu der Sounddatei + * @throws Exception + */ + private void playSound (String path) throws Exception { + //AudioInputStream audioStream = AudioSystem.getAudioInputStream(new File ("test.wav")); + AudioInputStream audioStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(path)); + + int BUFFER_SIZE = 128000; + AudioFormat audioFormat = null; + SourceDataLine sourceLine = null; + + audioFormat = audioStream.getFormat(); + + sourceLine = AudioSystem.getSourceDataLine(audioFormat); + sourceLine.open(audioFormat); + sourceLine.start(); + + int nBytesRead = 0; + byte[] abData = new byte[BUFFER_SIZE]; + while (nBytesRead != -1) { + try { + nBytesRead = + audioStream.read(abData, 0, abData.length); + } catch (IOException e) { + e.printStackTrace(); + } + + if (nBytesRead >= 0) { + int nBytesWritten = sourceLine.write(abData, 0, nBytesRead); + } + } + + sourceLine.drain(); + sourceLine.close(); + } +} diff --git a/src/sound/jaujau.wav b/src/sound/jaujau.wav new file mode 100755 index 0000000..fb15ccf Binary files /dev/null and b/src/sound/jaujau.wav differ