use of javax.sound.sampled.Clip in project JMRI by JMRI.
the class JavaSoundAudioSource method bindAudioBuffer.
@SuppressWarnings("SleepWhileInLoop")
@Override
boolean bindAudioBuffer(AudioBuffer audioBuffer) {
// First check we've been initialised
if (!initialised) {
return false;
}
// Wait for AudioBuffer to be loaded, or 20 seconds
long startTime = System.currentTimeMillis();
while (audioBuffer.getState() != AudioBuffer.STATE_LOADED && System.currentTimeMillis() - startTime < 20000) {
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}
}
if (audioBuffer instanceof JavaSoundAudioBuffer && audioBuffer.getState() == AudioBuffer.STATE_LOADED) {
// Cast to JavaSoundAudioBuffer to enable easier access to specific methods
JavaSoundAudioBuffer buffer = (JavaSoundAudioBuffer) audioBuffer;
// Get a JavaSound DataLine and Clip
DataLine.Info lineInfo;
lineInfo = new DataLine.Info(Clip.class, buffer.getAudioFormat());
Clip newClip;
try {
newClip = (Clip) mixer.getLine(lineInfo);
} catch (LineUnavailableException ex) {
log.warn("Error binding JavaSoundSource (" + this.getSystemName() + ") to AudioBuffer (" + this.getAssignedBufferName() + ") " + ex);
return false;
}
this.clip = newClip;
try {
clip.open(buffer.getAudioFormat(), buffer.getDataStorageBuffer(), 0, buffer.getDataStorageBuffer().length);
} catch (LineUnavailableException ex) {
log.warn("Error binding JavaSoundSource (" + this.getSystemName() + ") to AudioBuffer (" + this.getAssignedBufferName() + ")" + ex);
}
if (log.isDebugEnabled()) {
log.debug("Bind JavaSoundAudioSource (" + this.getSystemName() + ") to JavaSoundAudioBuffer (" + audioBuffer.getSystemName() + ")");
}
return true;
} else {
log.warn("AudioBuffer not loaded error when binding JavaSoundSource (" + this.getSystemName() + ") to AudioBuffer (" + this.getAssignedBufferName() + ")");
return false;
}
}
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 playn by threerings.
the class JavaAudio method createSound.
/**
* Creates a sound instance from the audio data available via {@code in}.
*
* @param rsrc a resource via which the audio data can be read.
* @param music if true, a custom {@link Clip} implementation will be used which can handle long
* audio clips; if false, the default Java clip implementation is used which cannot handle long
* audio clips.
*/
public JavaSound createSound(final JavaAssets.Resource rsrc, final boolean music) {
final JavaSound sound = new JavaSound();
((JavaPlatform) platform).invokeAsync(new Runnable() {
public void run() {
try {
AudioInputStream ais = rsrc.openAudioStream();
Clip clip = AudioSystem.getClip();
if (music) {
clip = new BigClip(clip);
}
AudioFormat baseFormat = ais.getFormat();
if (baseFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), // we have to force sample size to 16
16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), // big endian
false);
ais = AudioSystem.getAudioInputStream(decodedFormat, ais);
}
clip.open(ais);
dispatchLoaded(sound, clip);
} catch (Exception e) {
dispatchLoadError(sound, e);
}
}
});
return sound;
}
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)");
}
}
Aggregations