Skip to content

Commit

Permalink
Merge pull request #65 from joelpurra/beep-audioformat-detection
Browse files Browse the repository at this point in the history
Detect beep.wav audio format, use for playback
  • Loading branch information
farin committed Dec 15, 2014
2 parents e00df4b + c7afea0 commit 2f614cf
Showing 1 changed file with 66 additions and 6 deletions.
72 changes: 66 additions & 6 deletions src/main/java/com/jcloisterzone/ui/Client.java
Expand Up @@ -20,13 +20,19 @@
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
Expand Down Expand Up @@ -64,7 +70,6 @@
import com.jcloisterzone.ui.grid.GridPanel;
import com.jcloisterzone.ui.grid.KeyController;
import com.jcloisterzone.ui.grid.MainPanel;
import com.jcloisterzone.ui.grid.layer.PlacementHistory;
import com.jcloisterzone.ui.gtk.MenuFix;
import com.jcloisterzone.ui.panel.BackgroundPanel;
import com.jcloisterzone.ui.panel.ConnectGamePanel;
Expand Down Expand Up @@ -456,17 +461,72 @@ public void handleAbout() {
void beep() {
if (config.getBeep_alert()) {
try {
BufferedInputStream fileInStream = new BufferedInputStream(Client.class.getClassLoader().getResource("beep.wav").openStream());
AudioInputStream beepStream = AudioSystem.getAudioInputStream(fileInStream);
Clip c = AudioSystem.getClip();
c.open(beepStream);
c.start();
playResourceSound("beep.wav");
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}

/*
* Map of resource filenames to sound clip objects. TODO: clean up clip
* objects on destroy?
*/
private final Map<String, Clip> resourceSounds = new HashMap<String, Clip>();

/*
* Load and play sound clip from resources by filename.
*/
private void playResourceSound(String resourceFilename) throws IOException,
UnsupportedAudioFileException, LineUnavailableException {
// Load sound if necessary.
if (!resourceSounds.containsKey(resourceFilename)) {
BufferedInputStream resourceStream = loadResourceAsStream(resourceFilename);
Clip loadedClip = loadSoundFromStream(resourceStream);
resourceSounds.put(resourceFilename, loadedClip);
}

Clip clip = resourceSounds.get(resourceFilename);

// Stop before starting, in case it plays rapidly (haven't tested).
clip.stop();

// Always start from the beginning
clip.setFramePosition(0);
clip.start();
}

private BufferedInputStream loadResourceAsStream(String filename)
throws IOException {
BufferedInputStream resourceStream = new BufferedInputStream(
Client.class.getClassLoader().getResource(filename)
.openStream());

return resourceStream;
}

/*
* Pre-load sound clip so it can play from memory.
*/
private Clip loadSoundFromStream(BufferedInputStream inputStream)
throws UnsupportedAudioFileException, IOException,
LineUnavailableException {
AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(inputStream);

// Auto-detect file format.
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);

Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(audioInputStream);

// Don't need the stream anymore.
audioInputStream.close();

return clip;
}

void clearActions() {
ControlPanel controlPanel = gamePanel.getControlPanel();
ActionPanel ap = controlPanel.getActionPanel();
Expand Down

0 comments on commit 2f614cf

Please sign in to comment.