/** * Plays a sound from a file. * * @param filename Path to the sound file */ public static void playSound(String filename) { URL resource = ClassLoader.getSystemClassLoader().getResource(filename); try { final Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class)); clip.addLineListener(event -> { if (event.getType() == LineEvent.Type.STOP) { clip.close(); } }); clip.open(AudioSystem.getAudioInputStream(resource)); clip.start(); } catch (Exception e) { LOGGER.error("Failed to play sound " + filename, e); } }
/** * List Mixers. Only Mixers that support either TargetDataLines or SourceDataLines are listed, depending on the value of * bPlayback. * * @param bPlayback * bPlayback */ public static void listMixersAndExit(boolean bPlayback) { out("Available Mixers:"); Mixer.Info[] aInfos = AudioSystem.getMixerInfo(); for (int i = 0; i < aInfos.length; i++) { Mixer mixer = AudioSystem.getMixer(aInfos[i]); Line.Info lineInfo = new Line.Info(bPlayback ? SourceDataLine.class : TargetDataLine.class); if (mixer.isLineSupported(lineInfo)) { out(aInfos[i].getName()); } } if (aInfos.length == 0) { out("[No mixers available]"); } System.exit(0); }
/** * List Mixers. Only Mixers that support either TargetDataLines or SourceDataLines are listed, depending on the value of * bPlayback. * * @param bPlayback * bPlayback */ public static void listMixersAndExit(boolean bPlayback) { out("Available Mixers:"); Mixer.Info[] aInfos = AudioSystem.getMixerInfo(); for (int i = 0; i < aInfos.length; i++) { Mixer mixer = AudioSystem.getMixer(aInfos[i]); Line.Info lineInfo = new Line.Info(bPlayback ? SourceDataLine.class : TargetDataLine.class); if (mixer.isLineSupported(lineInfo)) { out(aInfos[i].getName()); } } if (aInfos.length == 0) { out("[No mixers available]"); } System.exit(0); }
Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); clip = (Clip) line;
AudioFormat format = new AudioFormat(22050, 16, 2, true, false); Line.Info info = new Line.Info(SourceDataLine.class);
Mixer mixer = AudioSystem.getMixer(null); sdl = (SourceDataLine) mixer.getLine(new Line.Info(SourceDataLine.class)); sdl.open(new AudioFormat(rate, 16, 2, true, false), bufferSize * 2 * 2 * 2 * 2 * 2); soundBuffer.order(ByteOrder.LITTLE_ENDIAN);
import java.io.File; import javax.sound.sampled.*; public void play(File file) { try { final Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class)); clip.addLineListener(new LineListener() { @Override public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) clip.close(); } }); clip.open(AudioSystem.getAudioInputStream(file)); clip.start(); } catch (Exception exc) { exc.printStackTrace(System.out); } }
/** * Get all available sources from which we may collect entropy * * @return audio sources */ protected static List<AudioSource> getSources() { List<AudioSource> list = new ArrayList<>(); // A recordable line is a target data line Line.Info targetDataType = new Line.Info(TargetDataLine.class); // The source will be from some mixer, so loop over all of them. // Get available mixers Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo(); for (Mixer.Info mi : mixerInfos) { // May throw SecurityException Mixer mixer = AudioSystem.getMixer(mi); // No use if cannot record from it if (!mixer.isLineSupported(targetDataType)) { continue; } getSources(list, mixer); } return list; }
import java.io.File; import javax.sound.sampled.*; public void play(File file) { try { final Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class)); clip.addLineListener(new LineListener() { @Override public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) clip.close(); } }); clip.open(AudioSystem.getAudioInputStream(file)); clip.start(); } catch (Exception exc) { exc.printStackTrace(System.out); } }
/** * Creates new AudioClip. If the clip cannot be opened no * exception is thrown, instead the methods of the AudioClip * return without performing any operations. * * @param location the clip location */ public AppletAudioClip(URL location) { try { AudioInputStream stream = AudioSystem.getAudioInputStream(location); clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class)); clip.open(stream); } catch (Exception ex) { System.err.println("Error loading sound:"+location.toString()); clip = null; } }
Clip sound = (Clip) AudioSystem.getLine(new Line.Info(Clip.class)); sound.open(AudioSystem.getAudioInputStream(file));
private void mixerComboBoxActionPerformed() { Mixer mixer = AudioSystem.getMixer((Mixer.Info) mixerComboBox.getSelectedItem()); Line.Info lineInfo = mixer.getSourceLineInfo(new Line.Info(Clip.class))[0]; boolean volumeSupported; boolean panSupported; try { Line line = mixer.getLine(lineInfo); volumeSupported = line.isControlSupported(FloatControl.Type.MASTER_GAIN); panSupported = line.isControlSupported(FloatControl.Type.PAN); } catch (LineUnavailableException e) { volumeSupported = false; panSupported = false; } enableMixerVolumeCheckBox.setEnabled(volumeSupported); enableMixerPanCheckBox.setEnabled(panSupported); }
public static void main(String[] args) { // A recordable line is a target data line Line.Info targetDataType = new Line.Info(TargetDataLine.class); // Get available mixers Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo(); for (Mixer.Info mi : mixerInfos) { // May throw SecurityException Mixer mixer = AudioSystem.getMixer(mi); // No use if cannot record from it if (!mixer.isLineSupported(targetDataType)) { continue; } System.out.println("\nname= " + mi.getName()); System.out.println("desc= " + mi.getDescription()); System.out.println("vers= " + mi.getVersion()); System.out.println("vend= " + mi.getVendor()); } }
private JComboBox getMixerComboBox() { if (mixerComboBox == null) { Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); Line.Info clipLineInfo = new Line.Info(Clip.class); Vector<Mixer.Info> mixers = new Vector<Mixer.Info>(); for (Mixer.Info mi : mixerInfo) { if (AudioSystem.getMixer(mi).getSourceLineInfo(clipLineInfo).length > 0) { mixers.add(mi); } } mixerComboBox = new JComboBox(mixers); mixerComboBox.setRenderer(new MixerInfoCellRenderer()); mixerComboBox.addActionListener(eventHandler); } return mixerComboBox; }
static List<AudioFormat> getSupportedAudioFormats() { List<AudioFormat> result = new ArrayList<AudioFormat>(); for(Line.Info info : AudioSystem.getSourceLineInfo( new Line.Info(SourceDataLine.class))) { if(info instanceof SourceDataLine.Info) { result.addAll(Arrays.asList( ((SourceDataLine.Info)info).getFormats())); } } return result; }
private Clip getClip () throws LineUnavailableException { final Line.Info linfo = new Line.Info (Clip.class); final Line line = this.getLine (linfo); return (Clip) line; }
/** * List Mixers. Only Mixers that support either TargetDataLines or SourceDataLines are listed, depending on the value of * bPlayback. * * @param bPlayback * bPlayback */ public static void listMixersAndExit(boolean bPlayback) { out("Available Mixers:"); Mixer.Info[] aInfos = AudioSystem.getMixerInfo(); for (int i = 0; i < aInfos.length; i++) { Mixer mixer = AudioSystem.getMixer(aInfos[i]); Line.Info lineInfo = new Line.Info(bPlayback ? SourceDataLine.class : TargetDataLine.class); if (mixer.isLineSupported(lineInfo)) { out(aInfos[i].getName()); } } if (aInfos.length == 0) { out("[No mixers available]"); } System.exit(0); }
/** * List Mixers. Only Mixers that support either TargetDataLines or SourceDataLines are listed, depending on the value of * bPlayback. * * @param bPlayback * bPlayback */ public static void listMixersAndExit(boolean bPlayback) { out("Available Mixers:"); Mixer.Info[] aInfos = AudioSystem.getMixerInfo(); for (int i = 0; i < aInfos.length; i++) { Mixer mixer = AudioSystem.getMixer(aInfos[i]); Line.Info lineInfo = new Line.Info(bPlayback ? SourceDataLine.class : TargetDataLine.class); if (mixer.isLineSupported(lineInfo)) { out(aInfos[i].getName()); } } if (aInfos.length == 0) { out("[No mixers available]"); } System.exit(0); }