Skip to content

Commit 43cb196

Browse files
committed
Added new Audio Handler
Updated gradle to add new depo's
1 parent e6a270d commit 43cb196

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ sourceSets{
1616

1717
dependencies{
1818
compile 'org.apache.commons:commons-lang3:3.4'
19-
compile 'javazoom:jlayer:1.0.1'
19+
compile('com.googlecode.soundlibs:mp3spi:1.9.5-1'){
20+
exclude module: 'junit'
21+
}
2022
testCompile 'junit:junit:4.12'
2123
compile files('res/jars/JSplashScreen.jar')
2224
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.redomar.game.audio;
2+
3+
import javax.sound.sampled.AudioFormat;
4+
import javax.sound.sampled.AudioInputStream;
5+
import javax.sound.sampled.AudioSystem;
6+
import javax.sound.sampled.Clip;
7+
8+
9+
public class AudioHandler {
10+
11+
private Clip clip;
12+
13+
public AudioHandler(String path){
14+
try{
15+
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(path));
16+
AudioFormat baseformat = audioInputStream.getFormat();
17+
AudioFormat decodeFormat = new AudioFormat(
18+
AudioFormat.Encoding.PCM_SIGNED,
19+
baseformat.getSampleRate(), 16,
20+
baseformat.getChannels(),
21+
baseformat.getChannels() * 2,
22+
baseformat.getSampleRate(),
23+
false
24+
);
25+
AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(
26+
decodeFormat, audioInputStream);
27+
clip = AudioSystem.getClip();
28+
clip.open(decodedAudioInputStream);
29+
} catch (Exception e){
30+
System.err.println(e.getStackTrace());
31+
}
32+
}
33+
34+
public void play(){
35+
if(clip == null) return;
36+
stop();
37+
clip.setFramePosition(0);
38+
clip.start();
39+
}
40+
41+
public void stop() {
42+
if (clip.isRunning()) clip.stop();
43+
}
44+
45+
public void close(){
46+
stop();
47+
clip.close();
48+
}
49+
50+
}

0 commit comments

Comments
 (0)