use of javax.sound.sampled.AudioInputStream in project SimpleAsteroids by ljialin.
the class SoundManager method getClip.
public static Clip getClip(String filename) {
Clip clip = null;
try {
clip = AudioSystem.getClip();
AudioInputStream sample = AudioSystem.getAudioInputStream(new File(path + filename + ".wav"));
clip.open(sample);
} catch (Exception e) {
e.printStackTrace();
}
return clip;
}
use of javax.sound.sampled.AudioInputStream in project yamcs-studio by yamcs.
the class SoundSystem method beep.
/**
* Plays a beep, only if there is not currently a previous beep playing.
*/
public static synchronized void beep() {
try {
if (beepClip != null) {
return;
}
beepClip = AudioSystem.getClip();
InputStream in = SoundSystem.class.getResourceAsStream(BEEP_SOUND);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(new BufferedInputStream(in));
beepClip.open(audioIn);
beepClip.start();
// explicitly stop the clip when sound has stopped
beepClip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
event.getLine().close();
beepClip = null;
}
}
});
} catch (Exception e) {
System.out.println(e);
log.log(Level.WARNING, "Error playing beep sound", e);
}
}
use of javax.sound.sampled.AudioInputStream in project algs4 by kevin-wayne.
the class StdAudio method readByte.
// return data as a byte array
private static byte[] readByte(String filename) {
byte[] data = null;
AudioInputStream ais = null;
try {
// try to read from file
File file = new File(filename);
if (file.exists()) {
ais = AudioSystem.getAudioInputStream(file);
int bytesToRead = ais.available();
data = new byte[bytesToRead];
int bytesRead = ais.read(data);
if (bytesToRead != bytesRead)
throw new IllegalStateException("read only " + bytesRead + " of " + bytesToRead + " bytes");
} else // try to read from URL
{
URL url = StdAudio.class.getResource(filename);
ais = AudioSystem.getAudioInputStream(url);
int bytesToRead = ais.available();
data = new byte[bytesToRead];
int bytesRead = ais.read(data);
if (bytesToRead != bytesRead)
throw new IllegalStateException("read only " + bytesRead + " of " + bytesToRead + " bytes");
}
} catch (IOException e) {
throw new IllegalArgumentException("could not read '" + filename + "'", e);
} catch (UnsupportedAudioFileException e) {
throw new IllegalArgumentException("unsupported audio format: '" + filename + "'", e);
}
return data;
}
use of javax.sound.sampled.AudioInputStream in project algs4 by kevin-wayne.
the class StdAudio method stream.
// https://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
// play a wav or aif file
// javax.sound.sampled.Clip fails for long clips (on some systems)
private static void stream(String filename) {
SourceDataLine line = null;
// 4K buffer
int BUFFER_SIZE = 4096;
try {
InputStream is = StdAudio.class.getResourceAsStream(filename);
AudioInputStream ais = AudioSystem.getAudioInputStream(is);
AudioFormat audioFormat = ais.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat);
line.start();
byte[] samples = new byte[BUFFER_SIZE];
int count = 0;
while ((count = ais.read(samples, 0, BUFFER_SIZE)) != -1) {
line.write(samples, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} finally {
if (line != null) {
line.drain();
line.close();
}
}
}
use of javax.sound.sampled.AudioInputStream in project algs4 by kevin-wayne.
the class StdAudio method loop.
/**
* Loops an audio file (in .wav, .mid, or .au format) in a background thread.
*
* @param filename the name of the audio file
* @throws IllegalArgumentException if {@code filename} is {@code null}
*/
public static synchronized void loop(String filename) {
if (filename == null)
throw new IllegalArgumentException();
// code adapted from: http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java
try {
Clip clip = AudioSystem.getClip();
InputStream is = StdAudio.class.getResourceAsStream(filename);
AudioInputStream ais = AudioSystem.getAudioInputStream(is);
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (UnsupportedAudioFileException e) {
throw new IllegalArgumentException("unsupported audio format: '" + filename + "'", e);
} catch (LineUnavailableException e) {
throw new IllegalArgumentException("could not play '" + filename + "'", e);
} catch (IOException e) {
throw new IllegalArgumentException("could not play '" + filename + "'", e);
}
}
Aggregations