Audio Klasse + TestKlasse hinzugefügt

This commit is contained in:
Sebastian Kacza
2021-01-04 18:24:22 +01:00
parent e1ab2a3540
commit 6e99ba3bcc
3 changed files with 100 additions and 0 deletions

View File

@@ -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();
}
}

76
src/de/jaujau/daten/Audio.java Executable file
View File

@@ -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();
}
}

BIN
src/sound/jaujau.wav Executable file

Binary file not shown.