Skip to content

Commit

Permalink
~ fix completely unusable audio system
Browse files Browse the repository at this point in the history
  • Loading branch information
milanvanzanten committed Jul 2, 2015
1 parent 61afdc6 commit fa9ccaa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
44 changes: 13 additions & 31 deletions src/client/sound/SoundManager.java
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;
}

}
15 changes: 8 additions & 7 deletions src/util/ResourceManager.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package util;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -57,7 +54,7 @@ public class ResourceManager {

public static final String SOUNDS_CHAT_PATH = SOUNDS_PATH + "chat/";

public static AudioInputStream SOUND_SNAP;
public static Clip SOUND_SNAP;

static {

Expand Down Expand Up @@ -100,10 +97,14 @@ public class ResourceManager {

try {

SOUND_SNAP = AudioSystem.getAudioInputStream(ResourceManager.class.getResource(SOUNDS_CHAT_PATH + "snap.wav"));
// CHAT

SOUND_SNAP = AudioSystem.getClip();
SOUND_SNAP.open(AudioSystem.getAudioInputStream(ResourceManager.class.getResource(SOUNDS_CHAT_PATH + "snap.wav")));

} catch(UnsupportedAudioFileException e) { e.printStackTrace();
} catch(IOException e) { e.printStackTrace(); }
} catch(IOException e) { e.printStackTrace();
} catch(LineUnavailableException e) { e.printStackTrace(); }

}

Expand Down

0 comments on commit fa9ccaa

Please sign in to comment.