-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
~ fix completely unusable audio system
- Loading branch information
milanvanzanten
committed
Jul 2, 2015
1 parent
61afdc6
commit fa9ccaa
Showing
2 changed files
with
21 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,34 @@ | ||
package client.sound; | ||
|
||
import javax.sound.sampled.AudioInputStream; | ||
import javax.sound.sampled.AudioSystem; | ||
import javax.sound.sampled.Clip; | ||
import javax.sound.sampled.LineUnavailableException; | ||
import java.io.IOException; | ||
import javax.sound.sampled.*; | ||
|
||
/** | ||
* Here's a handler for all things audio. | ||
*/ | ||
public class SoundManager { | ||
|
||
/** | ||
* Start playback of an AudioInputStream. | ||
* @param audio The AudioInputStream to play back. | ||
* Start playback of an Clip. | ||
* @param clip The Clip to play back. | ||
* @return The clip playing the audio. Null if an exception occurs. | ||
*/ | ||
public static Clip play(AudioInputStream audio) { | ||
Clip clip = getClipForAudio(audio); | ||
if(clip != null) clip.start(); | ||
return clip; | ||
public static Clip play(Clip clip) { | ||
return loop(clip, 0); | ||
} | ||
|
||
/** | ||
* Start looped playback of an AudioInputStream. | ||
* @param audio The AudioInputStream to play back. | ||
* Start looped playback of an Clip. | ||
* @param clip The Clip to play back. | ||
* @param count How often the audio should be looped. | ||
* @return The clip looping the audio. Null if an exception occurs. | ||
*/ | ||
public static Clip loop(AudioInputStream audio, int count) { | ||
Clip clip = getClipForAudio(audio); | ||
if(clip != null) clip.loop(count); | ||
public static Clip loop(Clip clip, int count) { | ||
if(clip != null) { | ||
if(clip.isRunning()) clip.stop(); | ||
clip.setFramePosition(0); | ||
clip.loop(count); | ||
} | ||
return clip; | ||
} | ||
|
||
/** | ||
* Get a clip object preloaded with an AudioInputStream. | ||
* @param audio the AudioInputStream to preload. | ||
* @return A Clip preloaded with the audio. Null if an exception occurs. | ||
*/ | ||
public static Clip getClipForAudio(AudioInputStream audio) { | ||
try { | ||
Clip clip = AudioSystem.getClip(); | ||
clip.open(audio); | ||
return clip; | ||
} catch(LineUnavailableException e) { e.printStackTrace(); | ||
} catch(IOException e) { e.printStackTrace(); } | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters