Skip to content

Commit de815d3

Browse files
committed
Error and Test Handling for Audio Class
1 parent cb104a3 commit de815d3

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/com/redomar/game/audio/AudioHandler.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@ public AudioHandler(String path){
1414
}
1515

1616
public AudioHandler(File file){
17-
initiate(file.toString());
17+
try {
18+
if(file.toString() != ""){
19+
initiate(file.toString());
20+
} else {
21+
throw new NullPointerException();
22+
}
23+
} catch (NullPointerException e){
24+
System.err.println("Destination Cannot be empty");
25+
throw e;
26+
}
1827
}
1928

20-
public void initiate(String path){
29+
private void initiate(String path){
2130
try{
2231
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(path));
2332
AudioFormat baseformat = audioInputStream.getFormat();

src/com/redomar/game/entities/projectiles/Small.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
public class Small extends Projectile{
1111

1212
public static final int FIRE_RATE = 10;
13-
14-
public AudioHandler smallSound;
15-
public File file;
13+
private static final File smallShot = new File("/music/small.mp3");
14+
private static AudioHandler smallSound;
1615

1716
public Small(LevelHandler level, int x, int y, double dir) {
1817
super(level, x, y, dir);
@@ -23,9 +22,7 @@ public Small(LevelHandler level, int x, int y, double dir) {
2322
nx = speed * Math.cos(angle);
2423
ny = speed * Math.sin(angle);
2524

26-
file = new File("/music/small.mp3");
27-
28-
smallSound = new AudioHandler(file);
25+
smallSound = new AudioHandler(smallShot);
2926
smallSound.setVolume(-15);
3027
smallSound.play();
3128
}

src/com/redomar/game/menu/Menu.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@ public static void play() {
4747
splash.toFront();
4848
splash.requestFocus();
4949
splash.splashOn();
50-
splash.setProgress(10, "Initializing SFX");
51-
52-
splash.setProgress(30, "Loading Music");
50+
splash.setProgress(20, "Loading Music");
5351
Game.setBackgroundMusic(new AudioHandler("/music/Towards The End.mp3"));
54-
splash.setProgress(40, "Setting Volume");
52+
splash.setProgress(50, "Setting Volume");
5553
Game.getBackgroundMusic().setVolume(-20);
56-
splash.setProgress(50, "Acquiring data: Multiplayer");
54+
splash.setProgress(60, "Acquiring data: Multiplayer");
5755
Thread.sleep(125);
5856
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
5957
String multiMsg = "Sorry but multiplayer has been disabled on this version.\nIf you would like multiplayer checkout Alpha 1.6";

test/com/redomar/game/audio/AudioHandlerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ public void bgMusicExists() throws Exception {
1919
assertTrue(sfx.exists());
2020
}
2121

22+
@Test(expected = NullPointerException.class)
23+
public void expectReturnExceptionFileEmptyDir(){
24+
File empty = new File("");
25+
AudioHandler audio = new AudioHandler(empty);
26+
}
27+
2228
}

0 commit comments

Comments
 (0)