File tree Expand file tree Collapse file tree 2 files changed +53
-4
lines changed
src/com/redomar/game/audio Expand file tree Collapse file tree 2 files changed +53
-4
lines changed Original file line number Diff line number Diff line change 1+ package com .redomar .game .audio ;
2+
3+ import javax .sound .sampled .*;
4+
5+ /**
6+ * For uncompressed files like .wav
7+ */
8+ public class AudioEffect {
9+
10+ Clip clip ;
11+
12+ public AudioEffect (String path ){
13+ try {
14+ AudioInputStream audioInputStream = AudioSystem .getAudioInputStream (getClass ().getResourceAsStream (path ));
15+ AudioFormat baseformat = audioInputStream .getFormat ();
16+ AudioInputStream decodedAudioInputStream = AudioSystem .getAudioInputStream (
17+ baseformat , audioInputStream );
18+ clip = AudioSystem .getClip ();
19+ clip .open (decodedAudioInputStream );
20+ } catch (Exception e ){
21+ System .err .println (e .getStackTrace ());
22+ }
23+ }
24+
25+ public void play (){
26+ if (clip == null ) return ;
27+ stop ();
28+ clip .setFramePosition (0 );
29+ clip .start ();
30+ }
31+
32+ public void setVolume (float velocity ){
33+ FloatControl volume = (FloatControl ) clip .getControl (FloatControl .Type .MASTER_GAIN );
34+ volume .setValue (velocity );
35+
36+ }
37+
38+ public void stop () {
39+ if (clip .isRunning ()) clip .stop ();
40+ }
41+
42+ public void close (){
43+ stop ();
44+ clip .close ();
45+ }
46+ }
Original file line number Diff line number Diff line change 11package com .redomar .game .audio ;
22
3- import javax .sound .sampled .AudioFormat ;
4- import javax .sound .sampled .AudioInputStream ;
5- import javax .sound .sampled .AudioSystem ;
6- import javax .sound .sampled .Clip ;
3+ import javax .sound .sampled .*;
74
85
96public class AudioHandler {
@@ -38,6 +35,12 @@ public void play(){
3835 clip .start ();
3936 }
4037
38+ public void setVolume (float velocity ){
39+ FloatControl volume = (FloatControl ) clip .getControl (FloatControl .Type .MASTER_GAIN );
40+ volume .setValue (velocity );
41+
42+ }
43+
4144 public void stop () {
4245 if (clip .isRunning ()) clip .stop ();
4346 }
You can’t perform that action at this time.
0 commit comments