use of javax.sound.sampled.Clip in project openblocks by mikaelhg.
the class SoundManager method loadSound.
public static Sound loadSound(String soundFileName) {
final URL url = SoundManager.class.getResource(soundFileName);
if (url == null) {
System.out.println("Could not find resource " + soundFileName);
return null;
}
AudioInputStream audioInputStream;
try {
audioInputStream = AudioSystem.getAudioInputStream(url);
} catch (UnsupportedAudioFileException e) {
return null;
} catch (IOException e) {
return null;
}
final AudioFormat format = audioInputStream.getFormat();
final Clip clip;
try {
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(audioInputStream);
} catch (LineUnavailableException e) {
System.out.println("Sorry, sound is not available");
return null;
} catch (IOException e) {
return null;
}
return new Sound(clip);
}
use of javax.sound.sampled.Clip in project intellij-community by JetBrains.
the class UIUtil method playSoundFromStream.
public static void playSoundFromStream(final Factory<InputStream> streamProducer) {
new Thread(new Runnable() {
// The wrapper thread is unnecessary, unless it blocks on the
// Clip finishing; see comments.
@Override
public void run() {
try {
Clip clip = AudioSystem.getClip();
InputStream stream = streamProducer.create();
if (!stream.markSupported())
stream = new BufferedInputStream(stream);
AudioInputStream inputStream = AudioSystem.getAudioInputStream(stream);
clip.open(inputStream);
clip.start();
} catch (Exception ignore) {
LOG.info(ignore);
}
}
}, "play sound").start();
}
use of javax.sound.sampled.Clip in project jdk8u_jdk by JetBrains.
the class ClipSetPos method main.
public static void main(String[] args) {
boolean testPassed = true;
Clip clip = null;
try {
clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, audioFormat));
clip.open(audioFormat, dataBuffer, 0, dataBuffer.length);
} catch (LineUnavailableException ex) {
log(ex);
log("Cannot test (this is not failure)");
return;
} catch (IllegalArgumentException ex) {
log(ex);
log("Cannot test (this is not failure)");
return;
}
log("clip: " + clip.getClass().getName());
int len = clip.getFrameLength();
for (int pos = 0; pos < len; pos += (len / 100)) {
clip.setFramePosition(pos);
int curPos = clip.getFramePosition();
if (Math.abs(pos - curPos) > MAX_FRAME_DELTA) {
log("Tried to set pos to " + pos + ", but got back " + curPos);
testPassed = false;
} else {
log("Sucessfully set pos to " + pos);
}
}
clip.close();
if (testPassed) {
log("Test PASSED.");
} else {
log("Test FAILED.");
throw new RuntimeException("Test FAILED (see log)");
}
}
use of javax.sound.sampled.Clip in project Minim by ddf.
the class JSMinim method getAudioRecordingClip.
/** @deprecated */
public AudioRecording getAudioRecordingClip(String filename) {
Clip clip = null;
AudioMetaData meta = null;
AudioInputStream ais = getAudioInputStream(filename);
if (ais != null) {
AudioFormat format = ais.getFormat();
if (format instanceof MpegAudioFormat) {
AudioFormat baseFormat = format;
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
// converts the stream to PCM audio from mp3 audio
ais = getAudioInputStream(format, ais);
}
DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat());
if (AudioSystem.isLineSupported(info)) {
// Obtain and open the line.
try {
clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
} catch (Exception e) {
error("Error obtaining Javasound Clip: " + e.getMessage());
return null;
}
Map<String, Object> props = getID3Tags(filename);
long lengthInMillis = -1;
if (props.containsKey("duration")) {
Long dur = (Long) props.get("duration");
lengthInMillis = dur.longValue() / 1000;
}
meta = new MP3MetaData(filename, lengthInMillis, props);
} else {
error("File format not supported.");
return null;
}
}
if (meta == null) {
// this means we're dealing with not-an-mp3
meta = new BasicMetaData(filename, clip.getMicrosecondLength() / 1000, -1);
}
return new JSAudioRecordingClip(clip, meta);
}
use of javax.sound.sampled.Clip 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;
}
Aggregations