返回音频文件类型列表

发布于 2024-11-18 08:25:15 字数 518 浏览 2 评论 0原文

在回答这个问题时: 我想制作一个java程序,其中有一个组合框,显示文件夹中所有可用文件的标题

另一种实现答案的方法引起了我的注意;使用 AudioSystem.getAudioFileTypes() 返回指定文件夹中所有支持的音频文件。我是一个相当缺乏经验的编码员,无法将此方法集成到我给定的答案中

File someFolder = new File("pathname");

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

有人可以告诉我这是如何完成的吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2024-11-25 08:25:15

以下源代码将显示特定于 Java Sound 理解的文件类型的 JFileChooser。一旦用户选择任何声音剪辑,应用程序就会启动。将获取该目录中所有剪辑的列表并以组合形式显示它们。

声音剪辑列表

从组合中选择剪辑时,我们可以javax.sound.sample.Clip 中播放声音(或使用 Java Sound API 的其他方式),但我们选择这样做。对于 1.6+ 使用 Desktop 打开文件的“一行”(在系统默认播放器中)。

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文