use of javax.sound.sampled.AudioInputStream 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.AudioInputStream in project d54 by mitrisdev.
the class AudioProcessor method openChannel.
/**
* Open the audio channel so data can be captured. This must be called once before frameUpdate or the
* data accessors are called.
*/
public void openChannel() {
try {
line.open();
} catch (LineUnavailableException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
line.start();
line.drain();
input = new AudioInputStream(line);
}
use of javax.sound.sampled.AudioInputStream 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.AudioInputStream in project java-swing-tips by aterai.
the class LookAndFeelUtil method showMessageDialogAndPlayAudio.
public void showMessageDialogAndPlayAudio(Component p, String msg, String audioResource) {
URL url = Thread.currentThread().getContextClassLoader().getResource(audioResource);
if (url == null) {
UIManager.getLookAndFeel().provideErrorFeedback(p);
JOptionPane.showMessageDialog(p, audioResource + " not found");
return;
}
try (AudioInputStream ss = AudioSystem.getAudioInputStream(url);
Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, ss.getFormat()))) {
SecondaryLoop loop = p.getToolkit().getSystemEventQueue().createSecondaryLoop();
clip.addLineListener(e -> {
LineEvent.Type t = e.getType();
System.out.println(t);
if (Objects.equals(t, LineEvent.Type.STOP) || Objects.equals(t, LineEvent.Type.CLOSE)) {
loop.exit();
}
});
clip.open(ss);
clip.start();
JOptionPane.showMessageDialog(p, msg);
loop.enter();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
ex.printStackTrace();
UIManager.getLookAndFeel().provideErrorFeedback(p);
}
}
use of javax.sound.sampled.AudioInputStream in project java-swing-tips by aterai.
the class MainPanel method loadAndPlayAudio.
public void loadAndPlayAudio(String path) {
try (AudioInputStream sound = AudioSystem.getAudioInputStream(getClass().getResource(path));
Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, sound.getFormat()))) {
SecondaryLoop loop = Toolkit.getDefaultToolkit().getSystemEventQueue().createSecondaryLoop();
clip.addLineListener(e -> {
LineEvent.Type t = e.getType();
if (Objects.equals(t, LineEvent.Type.STOP) || Objects.equals(t, LineEvent.Type.CLOSE)) {
loop.exit();
}
});
clip.open(sound);
clip.start();
loop.enter();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
ex.printStackTrace();
Toolkit.getDefaultToolkit().beep();
}
}
Aggregations