mp3文件的tag查看程序
闲着无聊,想分类我本机的mp3文件,先写个查看程序,分类慢慢写.
无图无真相,还是上图吧,简易的,很多问题没有处理.
package com.javaeye.i2534;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;
/**
*
* @author lan
*
*/
public class MP3TagViewer extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JLabel pathLabel = null;
private JTextField pathTextField = null;
private JButton browseButton = null;
private JScrollPane jScrollPane = null;
private JPanel jPanel1 = null;
private JList fileList = null;
private JLabel nameLabel = null;
private JLabel artistLabel = null;
private JLabel albumLabel = null;
private JLabel yearLabel = null;
private JLabel commentLabel = null;
private JTextField nameTextField = null;
private JTextField artistTextField = null;
private JTextField albumTextField = null;
private JTextField yearTextField = null;
private JTextArea commentTextArea = null;
/**
* This is the default constructor
*/
public MP3TagViewer() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(560, 396);
this.setContentPane(getJContentPane());
this.setTitle("MP3管理工具");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
pathLabel = new JLabel();
pathLabel.setBounds(new Rectangle(36, 21, 54, 24));
pathLabel.setHorizontalAlignment(SwingConstants.RIGHT);
pathLabel.setText("文件夹");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(pathLabel, null);
jPanel.add(getPathTextField(), null);
jPanel.add(getBrowseButton(), null);
jPanel.add(getJScrollPane(), null);
jPanel.add(getJPanel1(), null);
}
return jPanel;
}
/**
* This method initializes pathTextField
*
* @return javax.swing.JTextField
*/
private JTextField getPathTextField() {
if (pathTextField == null) {
pathTextField = new JTextField();
pathTextField.setBounds(new Rectangle(112, 20, 311, 26));
}
return pathTextField;
}
/**
* This method initializes browseButton
*
* @return javax.swing.JButton
*/
private JButton getBrowseButton() {
if (browseButton == null) {
browseButton = new JButton();
browseButton.setBounds(new Rectangle(438, 21, 62, 22));
browseButton.setText("浏览");
browseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JFileChooser chooser = new JFileChooser(FileSystemView
.getFileSystemView());
chooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.addChoosableFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
} else if (f.isFile()) {
return f.getName().toLowerCase().endsWith(
".mp3");
}
return false;
}
@Override
public String getDescription() {
return "MP3 (.mp3)";
}
});
if (chooser.showOpenDialog(MP3TagViewer.this) == JFileChooser.APPROVE_OPTION) {
File dir = chooser.getSelectedFile();
getPathTextField().setText(dir.getAbsolutePath());
Set<File> set = new HashSet<File>();
walk(dir, set);
DefaultListModel model = (DefaultListModel) getFileList()
.getModel();
model.clear();
for (File f : set) {
model.addElement(new FileWrapper(f));
}
}
}
});
}
return browseButton;
}
private class FileWrapper {
File file;
public FileWrapper(File file) {
this.file = file;
}
@Override
public String toString() {
return file.getName();
}
}
private class Info {
private final Charset GBK = Charset.forName("GBK");
// 应该是128位
private byte[] tag;
public Info(byte[] tag) throws IllegalArgumentException {
this.tag = tag;
if (this.tag.length != 128) {
throw new IllegalArgumentException();
}
if (!isValid()) {
throw new IllegalArgumentException();
}
}
private boolean isValid() {
byte[] array = Arrays.copyOfRange(this.tag, 0, 3);
String s = new String(array, GBK);
return "TAG".equals(s);
}
public String getArtist() {
return getString(33, 63);
}
public String getAlbum() {
return getString(63, 93);
}
public String getName() {
return getString(3, 33);
}
public String getYear() {
return getString(93, 97);
}
public String getComment() {
return getString(97, 125);
}
private String getString(int from, int to) {
byte[] array = Arrays.copyOfRange(this.tag, from, to);
int length = 0;
for (byte b : array) {
if (b != 0) {
length++;
} else {
break;
}
}
return new String(array, 0, length, GBK);
}
}
private void walk(File file, Collection<File> files) {
if (file.isHidden()) {
return;
}
if (file.isDirectory()) {
for (File f : file.listFiles()) {
this.walk(f, files);
}
} else if (file.isFile()) {
if (file.getName().toLowerCase().endsWith(".mp3")) {
files.add(file);
}
}
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(23, 75, 197, 265));
jScrollPane.setViewportView(getFileList());
jScrollPane.setBorder(BorderFactory.createTitledBorder(null,
"MP3\u5217\u8868", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
}
return jScrollPane;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
commentLabel = new JLabel();
commentLabel.setBounds(new Rectangle(25, 190, 50, 20));
commentLabel.setText("信息");
yearLabel = new JLabel();
yearLabel.setBounds(new Rectangle(25, 150, 50, 20));
yearLabel.setText("年代");
albumLabel = new JLabel();
albumLabel.setBounds(new Rectangle(25, 110, 50, 20));
albumLabel.setText("专辑");
artistLabel = new JLabel();
artistLabel.setBounds(new Rectangle(25, 70, 50, 20));
artistLabel.setText("演唱");
nameLabel = new JLabel();
nameLabel.setBounds(new Rectangle(25, 30, 50, 20));
nameLabel.setText("名称");
jPanel1 = new JPanel();
jPanel1.setLayout(null);
jPanel1.setBounds(new Rectangle(257, 75, 256, 264));
jPanel1.setBorder(BorderFactory.createTitledBorder(null,
"MP3\u4fe1\u606f", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 12), new Color(51, 51, 51)));
jPanel1.add(nameLabel, null);
jPanel1.add(artistLabel, null);
jPanel1.add(albumLabel, null);
jPanel1.add(yearLabel, null);
jPanel1.add(commentLabel, null);
jPanel1.add(getNameTextField(), null);
jPanel1.add(getArtistTextField(), null);
jPanel1.add(getAlbumTextField(), null);
jPanel1.add(getYearTextField(), null);
jPanel1.add(getCommentTextArea(), null);
}
return jPanel1;
}
/**
* This method initializes fileList
*
* @return javax.swing.JList
*/
private JList getFileList() {
if (fileList == null) {
fileList = new JList(new DefaultListModel());
fileList
.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(
javax.swing.event.ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
Object o = fileList.getSelectedValue();
if (o instanceof FileWrapper) {
File f = ((FileWrapper) o).file;
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(f, "r");
byte[] array = new byte[128];
raf.seek(raf.length() - 128);
raf.read(array);
Info info = new Info(array);
getNameTextField().setText(
info.getName());
getArtistTextField().setText(
info.getArtist());
getAlbumTextField().setText(
info.getAlbum());
getYearTextField().setText(
info.getYear());
getCommentTextArea().setText(
info.getComment());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} finally {
if (raf != null) {
try {
raf.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
}
});
}
return fileList;
}
/**
* This method initializes nameTextField
*
* @return javax.swing.JTextField
*/
private JTextField getNameTextField() {
if (nameTextField == null) {
nameTextField = new JTextField();
nameTextField.setBounds(new Rectangle(95, 30, 130, 20));
nameTextField.setEditable(false);
}
return nameTextField;
}
/**
* This method initializes artistTextField
*
* @return javax.swing.JTextField
*/
private JTextField getArtistTextField() {
if (artistTextField == null) {
artistTextField = new JTextField();
artistTextField.setBounds(new Rectangle(95, 70, 130, 20));
artistTextField.setEditable(false);
}
return artistTextField;
}
/**
* This method initializes albumTextField
*
* @return javax.swing.JTextField
*/
private JTextField getAlbumTextField() {
if (albumTextField == null) {
albumTextField = new JTextField();
albumTextField.setBounds(new Rectangle(95, 110, 130, 20));
albumTextField.setEditable(false);
}
return albumTextField;
}
/**
* This method initializes yearTextField
*
* @return javax.swing.JTextField
*/
private JTextField getYearTextField() {
if (yearTextField == null) {
yearTextField = new JTextField();
yearTextField.setBounds(new Rectangle(95, 150, 130, 20));
yearTextField.setEditable(false);
}
return yearTextField;
}
/**
* This method initializes commentTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getCommentTextArea() {
if (commentTextArea == null) {
commentTextArea = new JTextArea();
commentTextArea.setBounds(new Rectangle(95, 190, 130, 60));
commentTextArea.setEditable(false);
commentTextArea.setEnabled(true);
commentTextArea.setLineWrap(true);
}
return commentTextArea;
}
public static void main(String[] args) {
MP3TagViewer m = new MP3TagViewer();
m.setLocationRelativeTo(null);
m.setVisible(true);
}
} // @jve:decl-index=0:visual-constraint="10,10"