Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following source will show a <code>JFileChooser</code> that is specific to file types understood by Java Sound. Once the user selects any sound clip, the app. will get a listing of all the clips in that directory and display them in a combo. </p> <p><img src="https://i.stack.imgur.com/PBXI7.png" alt="Sound clip list"></p> <p>On selecting a clip from the combo., we could <a href="https://stackoverflow.com/tags/javasound/info">play the sound in a <code>javax.sound.sample.Clip</code></a> (or other ways using the Java Sound API), but instead we opt. for the 1.6+ 'one-liner' of using <code>Desktop</code> to open the file (in the system default player).</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import javax.sound.sampled.*; import java.io.*; class GetSoundsByFileType { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { AudioFileFormat.Type[] formatTypes = AudioSystem.getAudioFileTypes(); String[] types = new String[formatTypes.length]; for(int ii=0; ii&lt;types.length; ii++) { types[ii] = formatTypes[ii].getExtension(); } FileTypesFilter fileTypesFilter = new FileTypesFilter(types); // Just to confuse things, JFileChooser accepts a // different type of filter! FileNameExtensionFilter extensionFilter = new FileNameExtensionFilter("Sound clips", types); JFileChooser fc = new JFileChooser(); fc.setAcceptAllFileFilterUsed(false); fc.addChoosableFileFilter(extensionFilter); int result = fc.showOpenDialog(null); if (result==JFileChooser.APPROVE_OPTION) { File startAt = fc.getSelectedFile(); startAt = startAt.getParentFile(); File[] files = startAt.listFiles(fileTypesFilter); final JComboBox clipCombo = new JComboBox(files); clipCombo.addActionListener( new ActionListener(){ // 1.6+ Desktop desktop = Desktop.getDesktop(); public void actionPerformed(ActionEvent ae) { try { desktop.open( (File)clipCombo.getSelectedItem() ); } catch(Exception e) { e.printStackTrace(); } } } ); JOptionPane.showMessageDialog(null, clipCombo); } } }); } } class FileTypesFilter implements FilenameFilter { private String[] types; FileTypesFilter(String[] types ) { this.types = types; } public boolean accept(File dir, String name) { for (String type:types) { if (name.toLowerCase().endsWith(type.toLowerCase())) { return true; } } return false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload