generated from sigonasr2/JavaProjectTemplate
parent
9fe872b7c2
commit
883649be4d
Binary file not shown.
Binary file not shown.
@ -0,0 +1,57 @@ |
|||||||
|
package sig.engine; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import javax.sound.sampled.AudioInputStream; |
||||||
|
import javax.sound.sampled.AudioSystem; |
||||||
|
import javax.sound.sampled.Clip; |
||||||
|
import javax.sound.sampled.LineUnavailableException; |
||||||
|
import javax.sound.sampled.UnsupportedAudioFileException; |
||||||
|
|
||||||
|
public class Sound { |
||||||
|
public final static File SOUNDS_FOLDER = new File("..","sounds"); |
||||||
|
AudioInputStream data; |
||||||
|
Clip c; |
||||||
|
boolean loop; |
||||||
|
public Sound(java.lang.String filename){ |
||||||
|
this(filename,false); |
||||||
|
} |
||||||
|
public Sound(java.lang.String filename,boolean loop) { |
||||||
|
this.loop=loop; |
||||||
|
try { |
||||||
|
data=AudioSystem.getAudioInputStream(new File(SOUNDS_FOLDER.getAbsolutePath(),filename)); |
||||||
|
c = AudioSystem.getClip(); |
||||||
|
c.open(data); |
||||||
|
System.out.println("Loaded sound for "+filename+"."); |
||||||
|
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
System.err.println("ERROR: Failed to load sound in "+filename+"."); |
||||||
|
} |
||||||
|
} |
||||||
|
public void play(){ |
||||||
|
c.setFramePosition(0); |
||||||
|
c.loop((loop)?Clip.LOOP_CONTINUOUSLY:0); |
||||||
|
} |
||||||
|
public boolean isPlaying(){ |
||||||
|
return c.isRunning(); |
||||||
|
} |
||||||
|
public void unload(){ |
||||||
|
try { |
||||||
|
c.close(); |
||||||
|
data.close(); |
||||||
|
c=null; |
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
public boolean canLoop(){ |
||||||
|
return loop; |
||||||
|
} |
||||||
|
public void setCanLoop(boolean canLoop) { |
||||||
|
loop=canLoop; |
||||||
|
if (c!=null) { |
||||||
|
c.loop((loop)?Clip.LOOP_CONTINUOUSLY:0); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue