Search code examples
javaaudiojavasoundfile-type

Returning a list of audio filetypes


In answering this question: I want to make a java program in which there is a combobox which displays the titles of all the files available in a folder

Another method of implementing the answer was brought to my attention; the usage of AudioSystem.getAudioFileTypes() to return all the supported audio files in the specified folder. I am a fairly inexperienced coder and am unable to integrate this method in my given answer

File someFolder = new File("pathname");

Object[] wavFiles = someFolder.listFiles(wavExtensionFilenameFilter);
JComboBox songComboBox = new JComboBox(wavFiles);

Can anyone show me how this would be done?


Solution

  • The following source will show a JFileChooser 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.

    Sound clip list

    On selecting a clip from the combo., we could play the sound in a javax.sound.sample.Clip (or other ways using the Java Sound API), but instead we opt. for the 1.6+ 'one-liner' of using Desktop to open the file (in the system default player).

    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<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;
        }
    }