返回音频文件类型列表 [英] Returning a list of audio filetypes

查看:141
本文介绍了返回音频文件类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答这个问题时:(或使用Java Sound API的其他方式),但我们选择。使用桌面的1.6+'one-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);
//只是为了混淆事物,JFileChooser接受一个
//不同类型的过滤器!
FileNameExtensionFilter extensionFilter =
new FileNameExtensionFilter(Sound clips,types);
JFileChooser fc = new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
fc.addC hoosableFileFilter(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.getDesktop();

public void actionPerformed(ActionEvent ae){
try {
desktop.open((File)clipCombo.getSelectedItem());
} catch(Exception e){
e.printStackTrace();
}
}
});

JOptionPane.showMessageDialog(null,clipCombo);
}
}
});
}
}

类FileTypesFilter实现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())){
返回true;
}
}
返回false;
}
}


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?

解决方案

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.

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

这篇关于返回音频文件类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆