English 中文(简体)
恢复音频文档类型清单
原标题:Returning a list of audio filetypes
最佳回答

以下来源将显示一个专用于 Java声所理解的各类文件的。 用户一旦选择了任何音响片,该表将列出该名录的所有剪辑,并在一只 com子上显示。

Sound clip list

在从 com体中挑选出一只单子时,我们可以https://stackoverflow.com/tags/java Sound/info>在javax.worth.sample.Clip上演音。 (或采用 Java声预报系统的其他方法),但选择使用<代码>Desktop的1.6+1-liner(在系统违约角色中)打开文档。

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;
    }
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签